1
00:00:00,080 --> 00:00:02,600
So here we are back in the lab.

2
00:00:02,600 --> 00:00:08,680
And what we're looking at here are parameters being set for our very own MCP server that we just wrote.

3
00:00:09,240 --> 00:00:16,640
So the command is you've run because that's exactly what we would type at a command line to run this

4
00:00:16,640 --> 00:00:17,320
module.

5
00:00:17,480 --> 00:00:20,680
You've run account server dot Pi.

6
00:00:20,920 --> 00:00:24,160
Just run the account server.py that I just showed you.

7
00:00:24,160 --> 00:00:30,040
And we know that when that's called it's going to create an MCP server, a fast MCP.

8
00:00:30,320 --> 00:00:31,840
It's going to call run.

9
00:00:31,840 --> 00:00:35,080
And that we've got some functions decorated as MCP tools.

10
00:00:35,560 --> 00:00:36,160
All right.

11
00:00:36,440 --> 00:00:42,320
So then we use the open AI agents SDK context manager with MCP Server Studio.

12
00:00:42,640 --> 00:00:44,480
And we pass in those parameters.

13
00:00:44,520 --> 00:00:46,000
Remember this timeout.

14
00:00:46,240 --> 00:00:50,280
And we then are going to call server list tools.

15
00:00:50,400 --> 00:00:52,960
So just by running this what's going to happen.

16
00:00:52,960 --> 00:00:55,040
It's going to create an MCP client.

17
00:00:55,160 --> 00:01:00,640
It's then going to spawn our MCP server by carrying out this instruction right here.

18
00:01:01,080 --> 00:01:04,600
And it's then going to ask it, what tools do you offer us?

19
00:01:04,840 --> 00:01:05,760
And we'll print them out.

20
00:01:05,760 --> 00:01:08,240
And we're hoping to see the things that we decorated.

21
00:01:08,560 --> 00:01:10,080
Uh, let's see if this works.

22
00:01:10,320 --> 00:01:11,400
Can it be that simple?

23
00:01:11,720 --> 00:01:12,520
Off it goes.

24
00:01:12,600 --> 00:01:13,800
Took 1.4 seconds.

25
00:01:13,800 --> 00:01:14,880
Let's just print that.

26
00:01:15,480 --> 00:01:16,360
And there we go.

27
00:01:16,400 --> 00:01:19,200
These are the functions that we just decorated.

28
00:01:19,200 --> 00:01:20,280
Get balance, get holdings.

29
00:01:20,280 --> 00:01:20,760
Buy shares.

30
00:01:20,760 --> 00:01:21,400
Sell shares.

31
00:01:21,440 --> 00:01:22,520
Change strategy.

32
00:01:22,680 --> 00:01:23,600
It's available.

33
00:01:23,960 --> 00:01:24,400
Wow.

34
00:01:24,920 --> 00:01:29,560
Um, so as you can see, it's not super easy, but it's also it's pretty easy.

35
00:01:30,040 --> 00:01:31,840
Okay, let's try and put this to action.

36
00:01:31,840 --> 00:01:33,520
Let's have some instructions.

37
00:01:33,520 --> 00:01:37,600
You're able to manage an account for a client and answer questions about the account.

38
00:01:37,640 --> 00:01:38,680
My name is Ed.

39
00:01:38,800 --> 00:01:40,360
My account is under the name Ed.

40
00:01:40,400 --> 00:01:42,720
What is my balance and my holdings?

41
00:01:42,880 --> 00:01:46,360
So it's going to need to make use of these tools.

42
00:01:46,640 --> 00:01:48,520
And we'll give it the latest model.

43
00:01:48,520 --> 00:01:49,200
Why not.

44
00:01:49,640 --> 00:01:51,720
Uh, let's take that.

45
00:01:51,960 --> 00:01:55,440
And now this again is the same code as before.

46
00:01:55,600 --> 00:01:58,720
We use the context manager with MCP Server Studio.

47
00:01:58,760 --> 00:01:59,960
We pass in our parameters.

48
00:01:59,960 --> 00:02:03,960
Let's put in that timeout client session timeout is 30.

49
00:02:04,600 --> 00:02:12,080
Um, and uh, we will pass in our instructions, our model, and we will then run a run and display

50
00:02:12,080 --> 00:02:12,640
the output.

51
00:02:12,640 --> 00:02:18,440
We're hoping that it's going to spawn the server, call the tools and be able to tell me about my six

52
00:02:18,480 --> 00:02:23,440
Amazon shares, uh, and the balance and everything like that.

53
00:02:23,880 --> 00:02:27,400
So enter your current cash balance is 8000.

54
00:02:27,440 --> 00:02:29,960
Your holdings includes six shares of Amazon.

55
00:02:30,080 --> 00:02:32,760
If you need any further details then let me know.

56
00:02:33,080 --> 00:02:40,800
So that is us successfully calling our very own MCP server that then calls our business logic.

57
00:02:40,800 --> 00:02:44,600
And now it's time to show you what it's like to write an MCP client.

58
00:02:44,600 --> 00:02:49,440
But I should first explain that it's not a common task to write an MCP client.

59
00:02:49,440 --> 00:02:51,360
You don't really need to do it anymore.

60
00:02:51,640 --> 00:02:57,120
When I when I first started working on this lab, the way that you worked with OpenAI agents SDK, they

61
00:02:57,120 --> 00:02:59,680
didn't sort of natively support MCP.

62
00:02:59,840 --> 00:03:04,810
You had to write your own client and then provide the tools into OpenAI agents SDK.

63
00:03:05,210 --> 00:03:11,730
And it was the day that I finished this project and checked it into GitHub and did the push that same

64
00:03:11,730 --> 00:03:18,290
day, about about 2 or 3 hours later, they released an update to OpenAI SDK that simplified everything

65
00:03:18,290 --> 00:03:20,050
and meant that all my code was useless.

66
00:03:20,730 --> 00:03:24,250
And you can, you can, you can look at the git timestamps if you don't believe me.

67
00:03:24,530 --> 00:03:25,170
It was crazy.

68
00:03:25,170 --> 00:03:27,770
I was I was infuriated, but it is a great thing.

69
00:03:27,770 --> 00:03:33,370
And basically it means that just with this, this context manager construct, you automatically get,

70
00:03:33,450 --> 00:03:36,490
uh, OpenAI SDK creating the client for you.

71
00:03:36,730 --> 00:03:40,570
But anyway, it's a good exercise to show you what it's like to make a client.

72
00:03:40,690 --> 00:03:44,450
And also this this construct works for tools.

73
00:03:44,450 --> 00:03:47,810
But if you want to use resources, I think you still need to write a client.

74
00:03:47,850 --> 00:03:48,890
And it's not so common.

75
00:03:48,890 --> 00:03:52,490
But but but we'll do it anyway and you'll see how it works.

76
00:03:52,770 --> 00:03:53,130
Okay.

77
00:03:53,170 --> 00:03:56,970
So this is in a Python module called Accounts clients.

78
00:03:57,930 --> 00:03:59,770
And this this is where the magic happens.

79
00:03:59,770 --> 00:04:01,250
That is no longer needed really.

80
00:04:01,610 --> 00:04:01,970
Uh.

81
00:04:02,010 --> 00:04:09,690
So this is this is a this is a an MCB client for use with my accounts, MCP server.

82
00:04:09,690 --> 00:04:15,810
So at the top here I specify these are the parameters that will be used to launch the MCP server.

83
00:04:15,850 --> 00:04:17,410
Now this could be something configurable.

84
00:04:17,410 --> 00:04:23,250
You could make a sort of generic MCP client that takes this as configuration and then and then spawns

85
00:04:23,250 --> 00:04:23,330
it.

86
00:04:23,330 --> 00:04:28,770
But for now this is just a fixed client for our accounts MCP server.

87
00:04:28,770 --> 00:04:31,210
And so this is an accounts MCP client.

88
00:04:31,530 --> 00:04:33,570
And so there are a few things we need to be able to do.

89
00:04:33,570 --> 00:04:35,450
We need to be able to list the tools.

90
00:04:35,490 --> 00:04:40,090
And so this first function here is an example of the function that lists tools.

91
00:04:40,090 --> 00:04:44,690
And we're basically using a bunch of anthropic code here for our client.

92
00:04:45,010 --> 00:04:48,530
And you can see that there's some context managers that we manage a session.

93
00:04:48,650 --> 00:04:50,210
We initialize the session.

94
00:04:50,210 --> 00:04:55,330
And then we call list tools on the session and return the tools.

95
00:04:55,370 --> 00:04:59,210
And so it's just sort of plumbing stuff that that you need to know about.

96
00:04:59,250 --> 00:05:03,810
If you want to write something that will contact your server and list the tools.

97
00:05:04,250 --> 00:05:06,530
This second one is actually calling a tool.

98
00:05:07,250 --> 00:05:12,330
This is how you go about reading a one of the resources, and this is reading the other resources.

99
00:05:12,330 --> 00:05:15,850
You can see I specify the resource name right there.

100
00:05:16,290 --> 00:05:19,290
Um, and then finally the types.

101
00:05:19,290 --> 00:05:27,570
The way that MCP returns tools is very similar to the JSON, the standard JSON that you use when you

102
00:05:27,610 --> 00:05:30,290
call a tool, as we did in week one.

103
00:05:30,450 --> 00:05:31,770
But it's not identical.

104
00:05:31,770 --> 00:05:34,450
There is some couple of slight differences.

105
00:05:34,450 --> 00:05:40,610
So if you're writing your own MCP client, you also have to know how to map between the MCP description

106
00:05:40,610 --> 00:05:46,370
of a tool and the kind of JSON description of a tool that's used generally when you're calling LMS.

107
00:05:46,530 --> 00:05:51,290
And that is what this this function here does that I painstakingly wrote.

108
00:05:51,290 --> 00:05:56,530
But all of this comes for free packaged in OpenAI agents SDK.

109
00:05:56,690 --> 00:06:00,770
So you don't need to do all of this, but you could look through it should you be interested and should

110
00:06:00,770 --> 00:06:05,250
you want to understand what does it mean to have your own MCP client?

111
00:06:05,290 --> 00:06:09,570
Okay, so now it remains for us to actually run the MCP client that we just created.

112
00:06:09,570 --> 00:06:11,450
So I import from that module.

113
00:06:11,450 --> 00:06:14,370
We just looked at some of the functions we just saw.

114
00:06:14,490 --> 00:06:19,650
And the first function I call is the list account tools function, which is the one that's going to

115
00:06:19,690 --> 00:06:25,010
spawn an MCP server and ask it for its tools that will come back as MCP tools.

116
00:06:25,330 --> 00:06:30,650
I'm then going to call the other function I showed you, where I take the MCP tools, and I reconstitute

117
00:06:30,650 --> 00:06:32,850
them as open AI tools.

118
00:06:32,890 --> 00:06:38,170
I turn them into these function tools, just as if I'd had a function and decorated it with app function

119
00:06:38,170 --> 00:06:38,450
tools.

120
00:06:38,450 --> 00:06:39,170
Same thing.

121
00:06:39,290 --> 00:06:40,930
And then I'm going to print that.

122
00:06:40,930 --> 00:06:45,250
So I'll print these two separately so you can see the results side by side.

123
00:06:45,650 --> 00:06:52,410
So this first line here, this is the actual MCP tools themselves with their description, the arguments

124
00:06:52,450 --> 00:06:53,130
of all the tools.

125
00:06:53,130 --> 00:06:55,690
And you can take a look at them should you wish.

126
00:06:56,130 --> 00:07:02,810
And underneath it is the same thing but reconstituted as a function tool, which is the kind of object

127
00:07:02,810 --> 00:07:05,180
that needs to be passed in to OpenAI.

128
00:07:05,740 --> 00:07:08,820
And indeed, that's what I'm going to do right here.

129
00:07:09,100 --> 00:07:16,060
So this is the way you had to do it before OpenAI built this, so that you can now you can just pass

130
00:07:16,060 --> 00:07:17,020
in the MCP servers.

131
00:07:17,020 --> 00:07:19,220
You don't have to build a client and do all of this stuff.

132
00:07:19,260 --> 00:07:23,820
But before that, in fact, very recently this was the way to do it.

133
00:07:23,860 --> 00:07:27,060
You'd actually have to pass in the tools themselves.

134
00:07:27,140 --> 00:07:33,020
So we've used our MCP client to find the tools and then we're passing them in here.

135
00:07:33,060 --> 00:07:37,660
So as I say, you just follow along for interest to see what this is doing.

136
00:07:37,660 --> 00:07:39,660
But it's not like you'd need to do this yourself.

137
00:07:39,980 --> 00:07:47,140
So as always, I've got a trace, and then I'll pass in my instructions and I will pass in the tools

138
00:07:47,140 --> 00:07:50,940
that when these tools are called, it's actually going to call the MCP client.

139
00:07:50,940 --> 00:07:55,580
That's going to call the MCP server that's going to actually run our business logic.

140
00:07:55,700 --> 00:07:55,980
Okay.

141
00:07:56,020 --> 00:07:58,980
So now we're going to run our MCP client like this.

142
00:07:59,180 --> 00:08:06,340
So we're passing in the tools we are calling the model and it has now responded.

143
00:08:06,620 --> 00:08:08,100
I asked what's my balance?

144
00:08:08,100 --> 00:08:09,860
And it's responded with the balance.

145
00:08:09,860 --> 00:08:14,940
And so just as a recap, what's going on there, we provided it with some tools right here.

146
00:08:14,940 --> 00:08:21,820
Those tools are in fact wrappers around MCP tools, which actually created an MCP client which launched

147
00:08:21,820 --> 00:08:28,180
our MCP server and which ran our function through the business logic on the MCP server and got back

148
00:08:28,180 --> 00:08:29,900
the results to the LM.

149
00:08:30,020 --> 00:08:32,860
So quite a lot was going on there behind the scenes with this.

150
00:08:32,900 --> 00:08:37,820
And as I say, you really shouldn't need to do this yourself because open AI agents SDK does it all

151
00:08:37,820 --> 00:08:38,340
for you.

152
00:08:38,340 --> 00:08:41,940
But it's useful to see how the plumbing works behind the scenes.

153
00:08:42,340 --> 00:08:44,860
Now here's another example of using the MCP client.

154
00:08:44,860 --> 00:08:50,620
This time we're using this read accounts resource client function that I wrote.

155
00:08:50,620 --> 00:08:52,460
And I think that you do need to do it this way.

156
00:08:52,460 --> 00:08:57,940
If you want to use resources, then that doesn't come for free with the OpenAI agents SDK package.

157
00:08:58,060 --> 00:09:01,820
So this is an example of reading the resource called editor.

158
00:09:02,100 --> 00:09:04,140
So let's see what happens if we run that.

159
00:09:04,420 --> 00:09:06,140
Let's run it through our client, through our server.

160
00:09:06,140 --> 00:09:07,980
And that's what comes back.

161
00:09:08,180 --> 00:09:11,180
This is a description of my account.

162
00:09:11,420 --> 00:09:17,020
And just to show you, I could also, of course, just import the business logic directly.

163
00:09:17,020 --> 00:09:19,980
And this is ending up just calling this function report.

164
00:09:19,980 --> 00:09:22,500
So this should give exactly the same answer.

165
00:09:22,500 --> 00:09:23,740
You could see the same stuff.

166
00:09:23,940 --> 00:09:27,100
So essentially what we've done is we've taken this piece of business logic.

167
00:09:27,100 --> 00:09:31,820
We've wrapped it in an MCP server to be available at a certain resource.

168
00:09:31,820 --> 00:09:36,900
We've written an MCP client that allows you to expose that resource.

169
00:09:37,060 --> 00:09:39,380
And that is what this function is.

170
00:09:39,540 --> 00:09:42,940
And so you can call this MCP client to get this.

171
00:09:42,940 --> 00:09:45,420
You could also just call the business logic directly.

172
00:09:45,700 --> 00:09:47,420
Um, and so why would you want to do this.

173
00:09:47,460 --> 00:09:53,700
Again it's if you wanted to share this resource with other people, it gives everyone a simple, streamlined

174
00:09:53,740 --> 00:09:59,180
way to access your resources rather than having to understand how your business logic works.

175
00:10:00,100 --> 00:10:07,990
Okay, that wraps up creating MCP servers and MCP clients with most of the emphasis on the servers.

176
00:10:08,030 --> 00:10:12,070
And I would now like to give you an exercise to go and create your own.

177
00:10:12,270 --> 00:10:19,150
So one super simple one to do would be to write an MCP server that can tell you the current date, give

178
00:10:19,150 --> 00:10:26,110
the current date, and you can expose it as a tool that you can equip OpenAI agent with so that it can

179
00:10:26,110 --> 00:10:27,110
find out the current date.

180
00:10:27,110 --> 00:10:31,590
And it can make sure that what it's the the content and the questions it's answering, it's doing so

181
00:10:31,630 --> 00:10:33,430
mindful of what the current date is.

182
00:10:33,670 --> 00:10:35,430
Um, and uh, yeah.

183
00:10:35,470 --> 00:10:43,190
Then then as a harder exercise, you could not only build that MCP server, but also build an MCP client

184
00:10:43,190 --> 00:10:44,270
to accompany it.

185
00:10:44,470 --> 00:10:50,550
Taking a look at exactly how I just did that for the accounts client compared with the accounts server,

186
00:10:50,550 --> 00:10:51,710
same approach.

187
00:10:51,750 --> 00:10:57,670
And then you could actually write something that looks like a native call to OpenAI, as we did in week

188
00:10:57,670 --> 00:11:01,990
one, like a simple native call with a tool as a JSON.

189
00:11:01,990 --> 00:11:07,110
And then you're getting really bare metal and you're seeing how you can call an MLM and have an MCP

190
00:11:07,110 --> 00:11:10,870
client and an MCP server where you're writing all the nuts and bolts.

191
00:11:10,990 --> 00:11:14,470
The only real use of that is to give you some direct exposure to it.

192
00:11:14,470 --> 00:11:16,110
So only do that if you're if you're interested.

193
00:11:16,150 --> 00:11:17,550
You won't have to do that day to day.

194
00:11:17,670 --> 00:11:19,790
But but it's a it's an interesting exercise.

195
00:11:20,110 --> 00:11:26,150
It's also worth pointing out that actually answering the current date isn't a great tool isn't super

196
00:11:26,150 --> 00:11:31,350
useful in the real world, because if you need to tell your agent about the current date, it's better

197
00:11:31,350 --> 00:11:36,550
just to state it in the prompt so that it always has it available so that the model doesn't have to

198
00:11:36,550 --> 00:11:41,030
go through the extra work of knowing to call your tool to collect the date.

199
00:11:41,190 --> 00:11:44,710
So if you feel like it, try make some more interesting tools.

200
00:11:44,750 --> 00:11:50,350
Have a shot at making a calculator that can do some calculation operation on two inputs or something

201
00:11:50,350 --> 00:11:50,950
like that.

202
00:11:51,030 --> 00:11:51,390
So.

203
00:11:51,390 --> 00:11:57,670
So try and make some interesting tool that appeals to you, and then write an MCP server and perhaps

204
00:11:57,670 --> 00:12:01,670
also explore writing an MCP client should you wish and enjoy that.

205
00:12:01,710 --> 00:12:05,270
And we will then go over to wrap up for today.