1
00:00:00,440 --> 00:00:03,320
And we're right back in cursor again, of course.
当然，我们又回到了光标处。

2
00:00:03,320 --> 00:00:11,120
And we're going into the week six folder and we're going to lab two to create our own MCP server and
我们将进入第六周的文件夹，我们将进行第二个实验来创建我们自己的 MCP 服务器，

3
00:00:11,120 --> 00:00:15,960
client, which as I put here, it's it's pretty simple, but it's not super simple.
客户端，正如我在这里所说的，它非常简单，但并不是超级简单。

4
00:00:16,400 --> 00:00:21,400
The reason people are excited about MCP is that it's so simple to use other tools, not necessarily
人们对 MCP 感到兴奋的原因是使用其他工具是如此简单，不一定

5
00:00:21,400 --> 00:00:23,320
to create MCP servers.
创建 MCP 服务器。

6
00:00:23,920 --> 00:00:26,680
The first thing we're going to do, though, is we're going to look at a Python module.
不过，我们要做的第一件事是查看 Python 模块。

7
00:00:26,680 --> 00:00:32,600
And I know that that many people would much rather spend more time with Python modules than in labs.
我知道很多人宁愿花更多的时间在 Python 模块上而不是在实验室里。

8
00:00:32,600 --> 00:00:34,040
And this week we'll be doing both.
本周我们将同时进行这两件事。

9
00:00:34,040 --> 00:00:35,360
We'll be starting in labs.
我们将从实验室开始。

10
00:00:35,360 --> 00:00:39,160
We will be moving to just Python code in modules.
我们将只使用模块中的 Python 代码。

11
00:00:39,280 --> 00:00:49,440
And there is a Python module called accounts, and it contains a ton of code for managing your account,
还有一个名为 accounts 的 Python 模块，它包含大量用于管理帐户的代码，

12
00:00:49,560 --> 00:00:54,720
where you can buy and sell shares, and where you have a balance, and where you can do things like
您可以在哪里买卖股票，在哪里有余额，以及在哪里可以做类似的事情

13
00:00:54,720 --> 00:01:01,080
calculate profit and loss, list transactions, and where you can do things like buy shares and sell
计算损益、列出交易以及您可以在哪里进行购买股票和出售股票等操作

14
00:01:01,080 --> 00:01:02,960
shares based on a market price.
以市场价格为基础的股票。

15
00:01:03,000 --> 00:01:08,520
Looks like some interesting business logic and hopefully you remember it seems familiar to you because
看起来像是一些有趣的业务逻辑，希望您记得它对您来说似乎很熟悉，因为

16
00:01:08,520 --> 00:01:15,240
this is, of course, code generated by our agent team in week three, our engineering team in Crewe.
当然，这是我们位于克鲁的工程团队在第三周生成的代码。

17
00:01:15,280 --> 00:01:21,080
I created this code right here, and you can tell because it's got sort of like comments and type hints.
我在这里创建了这段代码，您可以看出，因为它有类似注释和类型提示的内容。

18
00:01:21,080 --> 00:01:26,520
And you probably know that I'm a bit hacky with this stuff, and that I only write comments and type
你可能知道我对这些东西有点老套，而且我只写评论和打字

19
00:01:26,520 --> 00:01:28,200
hints when I'm when I'm forced.
当我被迫的时候，就会有提示。

20
00:01:28,640 --> 00:01:33,080
But here the our agent crew did a wonderful job.
但我们的代理团队在这里做得非常出色。

21
00:01:33,320 --> 00:01:39,440
Um, so it's cool that we are taking the code here that is being used by our agents.
嗯，很高兴我们在这里获取我们的代理正在使用的代码。

22
00:01:39,480 --> 00:01:45,880
Now, I've made a slight change to it that I've updated it so that it does save accounts in a database,
现在，我对其进行了轻微的更改，更新了它，以便它确实将帐户保存在数据库中，

23
00:01:45,880 --> 00:01:49,920
and I've separated that out into a separate module called database.
我将其分离到一个名为数据库的单独模块中。

24
00:01:51,400 --> 00:01:53,920
And this is just simply using SQLite.
而这只是简单地使用 SQLite。

25
00:01:53,920 --> 00:01:55,680
So this is very vanilla.
所以这很香草。

26
00:01:55,800 --> 00:01:59,840
But it allows you to read and write accounts as JSON objects.
但它允许您以 JSON 对象的形式读取和写入帐户。

27
00:01:59,840 --> 00:02:02,720
And that I've hooked up to accounts.py.
我已经连接到accounts.py。

28
00:02:02,920 --> 00:02:09,800
But apart from making tiny changes like that, it is basically untouched code written by our engineering
但除了像这样的微小改变之外，它基本上是我们的工程人员编写的未受影响的代码

29
00:02:09,840 --> 00:02:12,360
team of agents in week three.
第三周的特工团队。

30
00:02:12,800 --> 00:02:21,240
All right, so back to our lab, uh, for, for, uh, today, uh, I see it says here week two, day
好吧，回到我们的实验室，呃，为了，为了，呃，今天，呃，我看到这里写着第二周，一天

31
00:02:21,280 --> 00:02:21,480
two.
二。

32
00:02:21,480 --> 00:02:23,120
I think we'll change that right away.
我想我们会立即改变这一点。

33
00:02:23,160 --> 00:02:24,120
That seems better.
这似乎更好。

34
00:02:24,560 --> 00:02:27,320
Okay, so let's get started.
好的，让我们开始吧。

35
00:02:27,320 --> 00:02:29,000
We'll do some imports.
我们会做一些进口。

36
00:02:29,560 --> 00:02:34,960
We are now going to import this Python module account okay.
我们现在要导入这个 Python 模块帐户了。

37
00:02:35,000 --> 00:02:37,200
So now I can call account dot get.
现在我可以调用 account dot get 了。

38
00:02:37,200 --> 00:02:41,360
And if I pass in a name it gets the account with that id here I am.
如果我传入一个名称，它就会获取具有该 id 的帐户。

39
00:02:41,360 --> 00:02:42,680
This is my account.
这是我的帐户。

40
00:02:42,720 --> 00:02:45,760
Apparently I have a balance of $9,400.
显然我有 9,400 美元的余额。

41
00:02:45,760 --> 00:02:50,440
I have three Amazon shares and I have some transactions.
我拥有三股亚马逊股票，并且有一些交易。

42
00:02:50,760 --> 00:02:57,200
This is the coming back from the code written by our agents and I can call buy shares.
这是我们代理编写的代码返回的结果，我可以调用买入股票。

43
00:02:57,200 --> 00:02:59,360
I can buy three more shares of Amazon.
我可以再购买三股亚马逊股票。

44
00:02:59,360 --> 00:03:00,760
And I have to give a reason.
我必须给出一个理由。

45
00:03:00,760 --> 00:03:05,440
And we'll just rewind the clocks a bit and imagine this is a few years back and I'll say, I'm going
我们将时钟倒回一点，想象这是几年前的事，我会说，我要去

46
00:03:05,440 --> 00:03:09,440
to buy three shares of Amazon because this bookstore website looks promising.
购买亚马逊的三股股票，因为这个书店网站看起来很有前途。

47
00:03:11,600 --> 00:03:12,320
So there we go.
我们就这样吧。

48
00:03:12,520 --> 00:03:13,440
Uh, yeah.
呃，是的。

49
00:03:13,480 --> 00:03:15,240
If only I'd had that foresight.
要是我有这样的先见之明就好了。

50
00:03:15,480 --> 00:03:16,120
Uh, okay.
呃，好吧。

51
00:03:16,160 --> 00:03:20,600
So I bought three, and now I have six shares of Amazon in there.
所以我买了三股，现在我有六股亚马逊股票。

52
00:03:20,600 --> 00:03:23,800
And I can call account Report to get a report about it.
我可以致电帐户报告以获得有关此情况的报告。

53
00:03:23,880 --> 00:03:27,200
Uh, and all sorts of information, um, about it.
呃，还有各种各样的信息，嗯，关于它。

54
00:03:27,200 --> 00:03:32,480
And I can also call account list transactions to see those transactions right there.
我还可以调用帐户列表交易来查看这些交易。

55
00:03:32,480 --> 00:03:34,400
So this is just showing that we've got some code.
所以这只是表明我们已经有了一些代码。

56
00:03:34,400 --> 00:03:35,520
It was written by our agents.
这是我们的代理人写的。

57
00:03:35,520 --> 00:03:40,040
And we can operate it to do things like buying shares and listing transactions.
我们可以操作它来做一些事情，比如购买股票和上市交易。

58
00:03:40,440 --> 00:03:42,080
Now it's MCP server time.
现在是 MCP 服务器时间。

59
00:03:42,320 --> 00:03:45,360
So writing an MCP server is pretty easy.
所以编写 MCP 服务器非常容易。

60
00:03:45,400 --> 00:03:53,680
It's just boilerplate code that you use to wrap code you've already got into an MCP server and you use
它只是样板代码，您可以用它来包装已经进入 MCP 服务器的代码，然后使用

61
00:03:53,680 --> 00:03:56,600
some libraries provided by anthropic.
一些由 anthropic 提供的库。

62
00:03:56,880 --> 00:03:57,840
And let's have a look.
让我们看一下。

63
00:03:57,880 --> 00:04:02,000
I've created one, a Python module called Accountsservice.
我创建了一个名为 Accountsservice 的 Python 模块。

64
00:04:03,120 --> 00:04:04,440
Let's see what happens.
让我们看看会发生什么。

65
00:04:04,760 --> 00:04:10,000
So it begins by importing an anthropic class called fast MCP.
因此，它首先导入一个名为 fast MCP 的人类类。

66
00:04:10,440 --> 00:04:13,520
And we also import our business logic account.
我们还导入我们的业务逻辑帐户。

67
00:04:13,880 --> 00:04:23,200
And we then create an MCP server by saying fast MCP account server that is creating a new fast MCP server
然后我们通过说快速 MCP 帐户服务器来创建一个 MCP 服务器，该服务器正在创建一个新的快速 MCP 服务器

68
00:04:23,200 --> 00:04:24,240
with that name.
用那个名字。

69
00:04:24,800 --> 00:04:33,080
What we then have is a number of functions listed here which are decorated with at MCP tool.
然后我们得到的是这里列出的一些函数，这些函数是用 MCP 工具修饰的。

70
00:04:33,400 --> 00:04:37,360
And for each of them we have a function like Getbalance.
对于它们中的每一个，我们都有一个类似 Getbalance 的函数。

71
00:04:37,520 --> 00:04:39,400
We describe that function.
我们描述该功能。

72
00:04:39,560 --> 00:04:43,480
We give information about it here using standard docstrings.
我们在这里使用标准文档字符串提供有关它的信息。

73
00:04:43,640 --> 00:04:45,240
And then we actually do that function.
然后我们实际执行该功能。

74
00:04:45,240 --> 00:04:48,160
And in our case we're just delegating to our business logic.
在我们的例子中，我们只是委托给我们的业务逻辑。

75
00:04:48,480 --> 00:04:53,520
But the idea is that this server will be spawned when we launch the MCP server.
但我们的想法是，当我们启动 MCP 服务器时，就会生成该服务器。

76
00:04:53,520 --> 00:04:58,160
This will be launched, and these tools will all be tools that will be available.
这个就要推出来了，这些工具都是可以使用的工具。

77
00:04:58,160 --> 00:05:02,600
And they will work simply by calling the business logic that we've imported right here.
它们只需调用我们在此处导入的业务逻辑即可工作。

78
00:05:02,960 --> 00:05:11,360
So we have Getbalance, get holdings, buy shares, sell shares, change strategy if we want to change
所以我们有Getbalance，持有股份，购买股票，出售股票，如果我们想改变就改变策略

79
00:05:11,360 --> 00:05:13,600
the strategy associated with the portfolio.
与投资组合相关的策略。

80
00:05:13,920 --> 00:05:17,280
And then I've also got some resources here.
然后我这里也有一些资源。

81
00:05:17,280 --> 00:05:21,360
As I say, resources are not as common as tools, but I do want to show you how they work.
正如我所说，资源不像工具那么常见，但我确实想向您展示它们是如何工作的。

82
00:05:21,400 --> 00:05:29,800
And here we have the ability to access a resource, the name of an account will just return its report,
在这里我们可以访问资源，帐户名只会返回其报告，

83
00:05:29,800 --> 00:05:34,040
and the strategy of an account will return its strategy.
账户的策略将返回其策略。

84
00:05:34,040 --> 00:05:40,520
And it gets like a sort of a fake URL, a URI like this, where you can describe what resource you want
它就像一种假 URL，像这样的 URI，您可以在其中描述您想要的资源

85
00:05:40,520 --> 00:05:41,360
to provide.
提供。

86
00:05:41,360 --> 00:05:47,840
If something requests this resource, and then down at the very bottom, we have the final piece here,
如果有东西请求这个资源，然后在最底部，我们在这里有最后的部分，

87
00:05:47,840 --> 00:05:55,120
which is that when this script, this Python script is run, what it should actually do is call the
也就是说，当这个脚本，这个Python脚本运行时，它实际上应该做的是调用

88
00:05:55,120 --> 00:06:01,840
run function on MCP and say that my transport mechanism is the usual stdio.
在 MCP 上运行函数并说我的传输机制是通常的 stdio。

89
00:06:02,400 --> 00:06:09,840
And by because we've done that, when this Python script is run, it will launch that MCP server, it
因为我们已经这样做了，所以当运行这个 Python 脚本时，它将启动该 MCP 服务器，它

90
00:06:09,840 --> 00:06:15,520
will import our business logic, and it will be ready to handle any of these tools.
将导入我们的业务逻辑，并且它将准备好处理任何这些工具。

91
00:06:15,520 --> 00:06:17,880
And as you can see, there's not much to this at all.
正如您所看到的，这根本没有什么意义。

92
00:06:17,880 --> 00:06:24,040
It's not super simple, but it's not hard at all to write your own MCP server that wraps your business
这不是超级简单，但编写自己的 MCP 服务器来包装您的业务一点也不难

93
00:06:24,040 --> 00:06:29,560
logic and launches a server like that, and now we just have to try and put it to use.
逻辑并启动这样的服务器，现在我们只需要尝试并使用它。
