And we're right back in cursor again, of course. 当然，我们又回到了光标处。
And we're going into the week six folder and we're going to lab two to create our own MCP server and 我们将进入第六周的文件夹，我们将进行第二个实验来创建我们自己的 MCP 服务器，
client, which as I put here, it's it's pretty simple, but it's not super simple. 客户端，正如我在这里所说的，它非常简单，但并不是超级简单。
The reason people are excited about MCP is that it's so simple to use other tools, not necessarily 人们对 MCP 感到兴奋的原因是使用其他工具是如此简单，不一定
to create MCP servers. 创建 MCP 服务器。
The first thing we're going to do, though, is we're going to look at a Python module. 不过，我们要做的第一件事是查看 Python 模块。
And I know that that many people would much rather spend more time with Python modules than in labs. 我知道很多人宁愿花更多的时间在 Python 模块上而不是在实验室里。
And this week we'll be doing both. 本周我们将同时进行这两件事。
We'll be starting in labs. 我们将从实验室开始。
We will be moving to just Python code in modules. 我们将只使用模块中的 Python 代码。
And there is a Python module called accounts, and it contains a ton of code for managing your account, 还有一个名为 accounts 的 Python 模块，它包含大量用于管理帐户的代码，
where you can buy and sell shares, and where you have a balance, and where you can do things like 您可以在哪里买卖股票，在哪里有余额，以及在哪里可以做类似的事情
calculate profit and loss, list transactions, and where you can do things like buy shares and sell 计算损益、列出交易以及您可以在哪里进行购买股票和出售股票等操作
shares based on a market price. 以市场价格为基础的股票。
Looks like some interesting business logic and hopefully you remember it seems familiar to you because 看起来像是一些有趣的业务逻辑，希望您记得它对您来说似乎很熟悉，因为
this is, of course, code generated by our agent team in week three, our engineering team in Crewe. 当然，这是我们位于克鲁的工程团队在第三周生成的代码。
I created this code right here, and you can tell because it's got sort of like comments and type hints. 我在这里创建了这段代码，您可以看出，因为它有类似注释和类型提示的内容。
And you probably know that I'm a bit hacky with this stuff, and that I only write comments and type 你可能知道我对这些东西有点老套，而且我只写评论和打字
hints when I'm when I'm forced. 当我被迫的时候，就会有提示。
But here the our agent crew did a wonderful job. 但我们的代理团队在这里做得非常出色。
Um, so it's cool that we are taking the code here that is being used by our agents. 嗯，很高兴我们在这里获取我们的代理正在使用的代码。
Now, I've made a slight change to it that I've updated it so that it does save accounts in a database, 现在，我对其进行了轻微的更改，更新了它，以便它确实将帐户保存在数据库中，
and I've separated that out into a separate module called database. 我将其分离到一个名为数据库的单独模块中。
And this is just simply using SQLite. 而这只是简单地使用 SQLite。
So this is very vanilla. 所以这很香草。
But it allows you to read and write accounts as JSON objects. 但它允许您以 JSON 对象的形式读取和写入帐户。
And that I've hooked up to accounts.py. 我已经连接到accounts.py。
But apart from making tiny changes like that, it is basically untouched code written by our engineering 但除了像这样的微小改变之外，它基本上是我们的工程人员编写的未受影响的代码
team of agents in week three. 第三周的特工团队。
All right, so back to our lab, uh, for, for, uh, today, uh, I see it says here week two, day 好吧，回到我们的实验室，呃，为了，为了，呃，今天，呃，我看到这里写着第二周，一天
two. 二。
I think we'll change that right away. 我想我们会立即改变这一点。
That seems better. 这似乎更好。
Okay, so let's get started. 好的，让我们开始吧。
We'll do some imports. 我们会做一些进口。
We are now going to import this Python module account okay. 我们现在要导入这个 Python 模块帐户了。
So now I can call account dot get. 现在我可以调用 account dot get 了。
And if I pass in a name it gets the account with that id here I am. 如果我传入一个名称，它就会获取具有该 id 的帐户。
This is my account. 这是我的帐户。
Apparently I have a balance of $9,400. 显然我有 9,400 美元的余额。
I have three Amazon shares and I have some transactions. 我拥有三股亚马逊股票，并且有一些交易。
This is the coming back from the code written by our agents and I can call buy shares. 这是我们代理编写的代码返回的结果，我可以调用买入股票。
I can buy three more shares of Amazon. 我可以再购买三股亚马逊股票。
And I have to give a reason. 我必须给出一个理由。
And we'll just rewind the clocks a bit and imagine this is a few years back and I'll say, I'm going 我们将时钟倒回一点，想象这是几年前的事，我会说，我要去
to buy three shares of Amazon because this bookstore website looks promising. 购买亚马逊的三股股票，因为这个书店网站看起来很有前途。
So there we go. 我们就这样吧。
Uh, yeah. 呃，是的。
If only I'd had that foresight. 要是我有这样的先见之明就好了。
Uh, okay. 呃，好吧。
So I bought three, and now I have six shares of Amazon in there. 所以我买了三股，现在我有六股亚马逊股票。
And I can call account Report to get a report about it. 我可以致电帐户报告以获得有关此情况的报告。
Uh, and all sorts of information, um, about it. 呃，还有各种各样的信息，嗯，关于它。
And I can also call account list transactions to see those transactions right there. 我还可以调用帐户列表交易来查看这些交易。
So this is just showing that we've got some code. 所以这只是表明我们已经有了一些代码。
It was written by our agents. 这是我们的代理人写的。
And we can operate it to do things like buying shares and listing transactions. 我们可以操作它来做一些事情，比如购买股票和上市交易。
Now it's MCP server time. 现在是 MCP 服务器时间。
So writing an MCP server is pretty easy. 所以编写 MCP 服务器非常容易。
It's just boilerplate code that you use to wrap code you've already got into an MCP server and you use 它只是样板代码，您可以用它来包装已经进入 MCP 服务器的代码，然后使用
some libraries provided by anthropic. 一些由 anthropic 提供的库。
And let's have a look. 让我们看一下。
I've created one, a Python module called Accountsservice. 我创建了一个名为 Accountsservice 的 Python 模块。
Let's see what happens. 让我们看看会发生什么。
So it begins by importing an anthropic class called fast MCP. 因此，它首先导入一个名为 fast MCP 的人类类。
And we also import our business logic account. 我们还导入我们的业务逻辑帐户。
And we then create an MCP server by saying fast MCP account server that is creating a new fast MCP server 然后我们通过说快速 MCP 帐户服务器来创建一个 MCP 服务器，该服务器正在创建一个新的快速 MCP 服务器
with that name. 用那个名字。
What we then have is a number of functions listed here which are decorated with at MCP tool. 然后我们得到的是这里列出的一些函数，这些函数是用 MCP 工具修饰的。
And for each of them we have a function like Getbalance. 对于它们中的每一个，我们都有一个类似 Getbalance 的函数。
We describe that function. 我们描述该功能。
We give information about it here using standard docstrings. 我们在这里使用标准文档字符串提供有关它的信息。
And then we actually do that function. 然后我们实际执行该功能。
And in our case we're just delegating to our business logic. 在我们的例子中，我们只是委托给我们的业务逻辑。
But the idea is that this server will be spawned when we launch the MCP server. 但我们的想法是，当我们启动 MCP 服务器时，就会生成该服务器。
This will be launched, and these tools will all be tools that will be available. 这个就要推出来了，这些工具都是可以使用的工具。
And they will work simply by calling the business logic that we've imported right here. 它们只需调用我们在此处导入的业务逻辑即可工作。
So we have Getbalance, get holdings, buy shares, sell shares, change strategy if we want to change 所以我们有Getbalance，持有股份，购买股票，出售股票，如果我们想改变就改变策略
the strategy associated with the portfolio. 与投资组合相关的策略。
And then I've also got some resources here. 然后我这里也有一些资源。
As I say, resources are not as common as tools, but I do want to show you how they work. 正如我所说，资源不像工具那么常见，但我确实想向您展示它们是如何工作的。
And here we have the ability to access a resource, the name of an account will just return its report, 在这里我们可以访问资源，帐户名只会返回其报告，
and the strategy of an account will return its strategy. 账户的策略将返回其策略。
And it gets like a sort of a fake URL, a URI like this, where you can describe what resource you want 它就像一种假 URL，像这样的 URI，您可以在其中描述您想要的资源
to provide. 提供。
If something requests this resource, and then down at the very bottom, we have the final piece here, 如果有东西请求这个资源，然后在最底部，我们在这里有最后的部分，
which is that when this script, this Python script is run, what it should actually do is call the 也就是说，当这个脚本，这个Python脚本运行时，它实际上应该做的是调用
run function on MCP and say that my transport mechanism is the usual stdio. 在 MCP 上运行函数并说我的传输机制是通常的 stdio。
And by because we've done that, when this Python script is run, it will launch that MCP server, it 因为我们已经这样做了，所以当运行这个 Python 脚本时，它将启动该 MCP 服务器，它
will import our business logic, and it will be ready to handle any of these tools. 将导入我们的业务逻辑，并且它将准备好处理任何这些工具。
And as you can see, there's not much to this at all. 正如您所看到的，这根本没有什么意义。
It's not super simple, but it's not hard at all to write your own MCP server that wraps your business 这不是超级简单，但编写自己的 MCP 服务器来包装您的业务一点也不难
logic and launches a server like that, and now we just have to try and put it to use. 逻辑并启动这样的服务器，现在我们只需要尝试并使用它。