WEBVTT

00:00.200 --> 00:00.640
Okay.

00:00.640 --> 00:02.080
Welcome back to the lab.

00:02.080 --> 00:07.360
Then we're at week two, day two, and we're going to run some imports and the load dot env.

00:07.680 --> 00:09.880
And now we're going to import account.

00:09.880 --> 00:11.640
This is the business logic class.

00:11.640 --> 00:14.760
Nothing to do with MCP servers yet I just want to show you this.

00:14.960 --> 00:19.520
When I import it it automatically creates a new SQL database with SQLite.

00:19.520 --> 00:21.680
Because that was in the code at the top.

00:21.680 --> 00:24.400
When you import, if it doesn't already exist, it creates one.

00:24.680 --> 00:30.640
And also if I get an account that doesn't exist, it creates a default account with $10,000 in it,

00:30.680 --> 00:31.480
which is kind.

00:31.560 --> 00:32.320
So there we go.

00:32.400 --> 00:33.440
This is all a simulation.

00:33.480 --> 00:33.760
Of course.

00:33.800 --> 00:37.760
It's just just to have something stored so we can watch trading happening.

00:37.920 --> 00:42.160
So here we go a new account called Ed with a fake balance of $10,000.

00:42.200 --> 00:45.640
No holdings, no trades, no time series.

00:45.880 --> 00:50.920
And we're now going to buy three shares of Amazon in this fake simulation.

00:51.120 --> 00:54.480
And we're going to pretend that I'm thinking of doing this years ago.

00:54.680 --> 01:01.080
And my my rationale for doing it is because this bookstore website looks promising.

01:01.600 --> 01:03.840
Oh, if only I had had that foresight.

01:04.120 --> 01:05.520
Uh, so there we go.

01:05.560 --> 01:09.000
We buy shares completed, and it gives some details of that.

01:09.000 --> 01:10.360
The balance, the strategy.

01:10.560 --> 01:15.720
And I should say it actually, there's some code in here that looks up the current share price of Amazon.

01:15.720 --> 01:18.360
And later we'll come back and I'll talk about how we do that.

01:19.040 --> 01:22.920
Then we are going to, uh, ask for a report of the account.

01:22.960 --> 01:28.040
And here it is with the strategy of the transactions, the rationale.

01:28.040 --> 01:33.000
And over at the end here, there is a total portfolio value and a profit and loss.

01:33.040 --> 01:38.280
And you'll notice that the total portfolio value is slightly less than the $10,000 it was originally.

01:38.280 --> 01:41.120
And there's a tiny negative, uh, profit and loss.

01:41.440 --> 01:47.680
And for those that care about this, that want to dig into this, uh, I have made a small assumption,

01:47.720 --> 01:54.200
a fixed assumption of, um, a fee that you pay every time that you trade in the form of what they call

01:54.240 --> 01:56.200
a a bid offer spread.

01:56.200 --> 01:57.400
And you'll see that in the code.

01:57.400 --> 02:03.050
It's just a very simplistic assumption to mean that every time you trade, you lose a little bit.

02:03.530 --> 02:03.970
Okay.

02:04.010 --> 02:04.810
So let's list.

02:04.850 --> 02:06.730
Transactions in our portfolio.

02:06.770 --> 02:08.530
We have three shares of Amazon at.

02:08.570 --> 02:09.370
That much each.

02:09.570 --> 02:12.410
And that is a quick demo of the business logic.

02:12.450 --> 02:19.050
The next step is to write is to is to use our MCP server and see if we can access this directly through

02:19.050 --> 02:20.170
the MCP server.

02:20.570 --> 02:21.170
All right.

02:21.170 --> 02:22.730
So how hard do you think it will be.

02:22.770 --> 02:24.490
It's going to be very easy.

02:24.850 --> 02:27.090
Uh so remember this params.

02:27.130 --> 02:30.130
Params is how you say what it is that you want to do.

02:30.170 --> 02:32.250
What what server do you want to run.

02:32.490 --> 02:35.450
And literally the command is you've.

02:35.730 --> 02:37.450
And the arguments is run.

02:37.450 --> 02:40.330
And then account server dot pi.

02:40.610 --> 02:43.650
It's just going to spawn a process.

02:43.970 --> 02:45.170
It's going to do it itself.

02:45.170 --> 02:46.570
It's going to run this process.

02:46.570 --> 02:49.450
You've run account server.py.

02:50.210 --> 02:50.770
Okay.

02:51.130 --> 02:56.530
Now I have this line async with MCP server studio as server.

02:56.530 --> 02:58.170
So what is MCP Server Studio.

02:58.170 --> 03:04.410
So this this is a class from OpenAI agents SDK.

03:04.770 --> 03:14.090
And it's basically some code which essentially creates an MCP client and attaches to an STD IO server

03:14.170 --> 03:15.570
that you want to run.

03:15.570 --> 03:21.490
It will it will spawn that server and then run it and allow us to do something with it.

03:21.490 --> 03:25.170
So all of that is simply in this one class.

03:25.490 --> 03:31.050
And as I think I mentioned before, this is actually relatively new in OpenAI agents SDK.

03:31.090 --> 03:36.930
And as of just recently, you would have to do that last part yourself of writing the code that spawns

03:36.930 --> 03:37.570
the server.

03:37.850 --> 03:41.210
And fear not, we will do that anyway, just so you see that side of it.

03:41.210 --> 03:44.890
But you don't need to because OpenAI agents SDK has written that for you.

03:45.050 --> 03:49.810
So this is the client and spawns that service.

03:50.090 --> 03:56.610
And then we are just going to call list tools, just as we did for fetch, for all of the other tools

03:56.610 --> 03:58.090
that we used last time.

03:58.090 --> 03:59.130
This is the same code.

03:59.130 --> 04:00.610
It's just three lines.

04:00.620 --> 04:04.220
we run it, we get the MCP tools and here they are.

04:04.340 --> 04:08.780
Get balance, get holdings, buy shares, sell shares with a description.

04:08.780 --> 04:14.140
And then you'll see if we scroll along that there are going to be all of the different bits and pieces

04:14.140 --> 04:14.740
to it.

04:14.900 --> 04:24.180
So if we now just remind ourselves if we go to the accounts server, you'll see here is our MCP server.

04:24.180 --> 04:30.740
And so get balance right here was one of the one of the decorated functions from our server.

04:30.740 --> 04:36.460
And if we go back here you'll see that get balance with that description is exactly what has been surfaced

04:36.460 --> 04:36.980
here.

04:37.180 --> 04:40.860
And so it really is that straightforward okay.

04:40.860 --> 04:43.860
So we've seen how easy it is to list the tools.

04:43.900 --> 04:45.140
Now let's use them.

04:45.340 --> 04:49.100
So it really is going to be again very straightforward.

04:49.380 --> 04:54.780
Uh writing MCP servers is a little bit of a few extra decorators here and there.

04:54.820 --> 04:56.540
Using them is a cinch.

04:57.060 --> 05:00.460
So here here's uh, some instructions or a system prompt.

05:00.460 --> 05:04.140
You are able to manage an account for a client and answer questions about the account.

05:04.180 --> 05:04.860
My request.

05:04.860 --> 05:05.900
My name is Ed.

05:05.940 --> 05:07.500
My account is under the name Ed.

05:07.540 --> 05:09.540
What's my balance and my holdings?

05:09.660 --> 05:11.540
Model GPT four mini.

05:11.780 --> 05:12.740
That sets us up.

05:12.980 --> 05:14.020
And so here it is.

05:14.020 --> 05:17.780
This is the code with MCP server studio.

05:17.820 --> 05:24.420
That is the OpenAI agents SDK thing passing in the parameters of our server, caching the tools.

05:24.420 --> 05:27.940
Why not uh, as uh, as MCP server.

05:28.300 --> 05:32.860
We don't need to do this, particularly for one call like this, especially to to show you how simple

05:32.860 --> 05:33.780
the code looks.

05:34.140 --> 05:38.580
Uh, so with an we create an agent called account manager.

05:38.580 --> 05:44.340
We give it the system, prompt the model, and we pass in the server, the MCP server that we want to

05:44.380 --> 05:48.140
use, which is our MCP server, our very own homemade one.

05:48.140 --> 05:54.860
And then we put in a trace and we say runner dot run with the agent and we pass in our request and we'll

05:54.860 --> 05:56.060
display the final output.

05:56.060 --> 05:57.940
The question is is this going to work?

05:57.940 --> 05:58.700
What it's going to do?

05:58.700 --> 06:04.830
If it works, it's going to create an MCP client, which will then spawn an MCP server.

06:04.830 --> 06:10.910
It will basically call you've run account Server.py launching that process.

06:10.910 --> 06:16.230
It will talk to that process using standard input and output, and it will collect the tools.

06:16.230 --> 06:22.390
It will pass those tools into our agent by constructing the JSON gumph that will go in.

06:22.630 --> 06:28.790
And when the LM says it wants to use a tool, then this code will then run that using standard input

06:28.790 --> 06:32.190
output to ask our spawned process to run that tool.

06:32.350 --> 06:35.790
And then the results will come back and be given to the LM which will complete.

06:35.790 --> 06:42.030
So there is a lot of stuff happening backwards and forwards that's all been elegantly wrapped into this.

06:42.070 --> 06:42.790
Will it work?

06:42.790 --> 06:50.190
Will we find out our holdings in natural language from asking GPT four mini based on our very own server?

06:50.470 --> 06:51.470
Yes we will.

06:51.790 --> 06:55.390
Your current balance is 9470.

06:55.430 --> 06:59.030
In terms of holdings, you have three shares of Amazon.

06:59.470 --> 07:01.350
That is mission accomplished.
