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.600 --> 00:32.360
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.100
And my my rationale for doing it is because this bookstore website looks promising.

01:01.580 --> 01:03.860
Oh, if only I had had that foresight.

01:04.140 --> 01:05.500
Uh, so there we go.

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

01:09.020 --> 01:10.340
The balance, the strategy.

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

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

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

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

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

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

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

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

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

01:54.260 --> 01:56.220
a a bid offer spread.

01:56.220 --> 01:57.420
And you'll see that in the code.

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

02:03.510 --> 02:03.990
Okay.

02:04.030 --> 02:04.830
So let's list.

02:04.870 --> 02:06.710
Transactions in our portfolio.

02:06.750 --> 02:08.510
We have three shares of Amazon at.

02:08.550 --> 02:09.390
That much each.

02:09.590 --> 02:12.390
And that is a quick demo of the business logic.

02:12.430 --> 02:19.070
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.070 --> 02:20.310
the MCP server.

02:20.350 --> 02:20.910
All right.

02:20.910 --> 02:22.470
So how hard do you think it will be.

02:22.550 --> 02:24.270
It's going to be very easy.

02:24.590 --> 02:26.830
Uh so remember this params.

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

02:29.910 --> 02:31.990
What what server do you want to run.

02:32.270 --> 02:35.230
And literally the command is you've.

02:35.470 --> 02:37.190
And the arguments is run.

02:37.190 --> 02:40.110
And then account server Pi.

02:40.390 --> 02:43.430
It's just going to spawn a process.

02:43.750 --> 02:44.910
It's going to do it itself.

02:44.910 --> 02:46.350
It's going to run this process.

02:46.350 --> 02:49.230
You've run account server dot Pi.

02:49.990 --> 02:50.550
Okay.

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

02:56.270 --> 02:57.950
So what is MCP Server Studio.

02:57.950 --> 03:04.170
So this this is a class from OpenAI agents SDK.

03:04.530 --> 03:13.850
And it's basically some code which essentially creates an MCP client and attaches to an STD I o server

03:13.970 --> 03:15.330
that you want to run.

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

03:21.250 --> 03:24.970
So all of that is simply in this one class.

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

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

03:36.690 --> 03:37.370
the server.

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

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

03:44.810 --> 03:49.570
So this is the client and spawns that service.

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

03:56.410 --> 03:57.850
that we used last time.

03:57.850 --> 03:58.850
This is the same code.

03:58.850 --> 04:00.370
It's just three lines.

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

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

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

04:13.910 --> 04:14.670
to it.

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

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

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

04:36.230 --> 04:36.750
here.

04:36.950 --> 04:40.830
And so it really is that straightforward okay.

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

04:43.870 --> 04:45.110
Now let's use them.

04:45.310 --> 04:49.070
So it really is going to be again very straightforward.

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

04:54.790 --> 04:56.470
Using them is a cinch.

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

05:00.490 --> 05:04.090
you are able to manage an account for a client and answer questions about the account.

05:04.130 --> 05:04.850
My request.

05:04.850 --> 05:05.850
My name is Ed.

05:05.890 --> 05:07.450
My account is under the name Ed.

05:07.490 --> 05:09.490
What's my balance and my holdings?

05:09.650 --> 05:11.490
Model GPT four mini.

05:11.730 --> 05:12.690
That sets us up.

05:12.970 --> 05:13.970
And so here it is.

05:13.970 --> 05:17.730
This is the code with MCP server studio.

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

05:24.370 --> 05:27.930
Why not uh, as uh, as MCP server?

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

05:32.850 --> 05:33.690
the code looks.

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

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

05:44.330 --> 05:48.090
use, which is r MCP server, our very own homemade one.

05:48.090 --> 05:52.730
And then we put in a trace and we say run a run with the agent.

05:52.730 --> 05:56.050
And we pass in our request and we'll display the final output.

05:56.050 --> 05:57.930
The question is is this going to work.

05:57.930 --> 05:58.650
What it's going to do?

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

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

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

06:16.180 --> 06:22.340
It will pass those tools into our agent by constructing the JSON gumpf that will go in.

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

06:28.740 --> 06:32.220
output to ask our spawned process to run that tool.

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

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

06:42.060 --> 06:42.780
Will it work?

06:42.780 --> 06:49.500
Will we find out our holdings in natural language from asking GPT four or mini based on our very own

06:49.540 --> 06:50.140
server?

06:50.460 --> 06:51.460
Yes we will.

06:51.740 --> 06:55.340
Your current balance is 9470.

06:55.420 --> 06:58.980
In terms of holdings, you have three shares of Amazon.

06:59.420 --> 07:01.140
That is mission accomplished.
