Hi, and welcome to this AI and C-Sharp video on the Microsoft Agent Framework.
Today we're going to look how you can create your own remote MCP server.
And MCP is as such not AI-based, but we are going to use one of the tools
and have an AI behind the scenes.
But as such, not too much about AI, it's just a protocol.
But let's go in and see.
So in here we have, under tools, calling and MCP,
we have an exposure agent as remote MCP.
And this is a web API, ASP.NET Core.
And in terms of NuGet package that we need,
we can see that we use OpenAI, Azure OpenAI,
but you can, of course, use whatever.
And then we're using the package called
ModelContextProtocol, ASP.NET Core, which is the one for hosting MCPs.
And MCPs is fairly simple.
In our case, the first thing we do is we make our client for the agent
and register that as a singleton.
But then we say, builder.services.addMCP
with HTTP transport, meaning remote,
and take all tools that are in disassembly and add them to the system.
We can also say with tool, if we want to specify each tool.
But in our case, we'll just take everything that have a certain attribute here.
Then we map MCP, and we give it slash MCP at the end.
Sort of a standard by many people, but we could call it whatever.
It's just a map point as normal.
And we have our tools, and I've made three tools.
So I just put them in a class that we don't really knew up by ourselves.
MCP will do that for us.
And we give it this MCP server tool type,
meaning that it should look in this class.
And we have our first MCP tool here,
which is just not using AI in any way.
And it's just a get the secret word, which is banana cake here.
We tell it that it's an MCP server tool, that the name is this.
And read only, meaning that it doesn't need to ask commissions
because it will only give back data, it will not change data.
And we can give it a description.
We'll see that when we use it in the various clients.
Then we have a tool more called Ask John the Pirate.
Just giving it a name, and again, it's read only.
So we just tell that John the Pirate can talk about pirate life.
And inside we're just newing up an agent,
and telling that he should answer children's questions about pirates.
So this is the only thing we're actually doing using Agent Framework 4,
because again, MCP is just a protocol.
The last tool we're making, we make read only equals false
because this is an add order, simulate something,
put something into a sales order, into an ERP system.
We're not really going to do it, but that would just be normal C-sharp code.
And we're returning the number of that order back to the system.
And that is actually everything we need in terms of code in order to do this.
So if we run it, we will see that it's just a normal,
once we get it up and running here.
Yeah, so we have it running here. It's both SMCP.
And now if we want to use it, we can use it in the various clients.
So let me show you three different clients,
but you can use all the clients that support MCP remote.
So the best tool to test MCPs in my book is Visual Studio Code,
because it has by far the best implementation of MCP.
It's been there the longest, and it shows.
The only thing MCP here can't do is, if we look at our tool here,
it's exposing itself on this port.
But as of this recording, there is a bug in Visual Studio Code
that it can't use these local hosts for MCPs as testing.
So what I have done here is I have turned on DevTunnels,
and I created a tunnel called AI Samples.
AI DevTunnels is a way of you getting a remote URL
that make a reverse proxy down to your local host.
So if we look here, it might say local host,
but if we go to our output and look under DevTunnels,
it will actually also be on this URL that is unique to me.
So if we take that URL and go to Visual Studio Code,
and say add MCP server, it needs to be an HTTP,
and we have this slash MCP.
We can call it myMCP,
and we just put it for this workspace.
This is just a folder called Hello MCP,
and what we see is it makes a mcp.json,
and it has started our tool, which will have the three tools,
and we can see down here in our copilot that our tool is here,
and we can see that our name of the tool is here,
and our description of a tool is here.
So now we can use it,
and we can, for example, say what is the secret word,
and if we do that, it runs the MCP tool,
and what we can do if we want to
is, of course, that we can set some breakpoints to see what's going on,
and let me take this and ask a new sign,
so it hit the breakpoint,
and come back with the secret word.
Similar, we can say, ask John why it's green.
Again, hit the breakpoint.
In this case, it will then run an AI, of course,
and it comes back with an answer,
and finally, if we say add order
to ACME for 10 anvils,
because this is a destructive tool or non-read-only tool,
it will come up and ask us,
are you sure you want to send customer ACME items to buy anvil,
and quantity 10, which was what we had for the three inputs for the order,
and if we say allow, it will be hit,
and will come back that the order has been added.
So Visual Studio Code is really, really good for testing this.
Another tool is, of course, Visual Studio Code itself.
And Visual Studio, yeah, they have the tool here in Copilot,
and you can say add,
and 9 out of 10 times this doesn't work.
It is fairly new, and they simply have done a very poor implementation.
The best way to actually get MCP working in Visual Studio
is to go to the Visual Studio part of it.
So it made this VS Code and MCP,
and take this folder,
and put it over in your Visual Studio project.
So I have my Visual Studio project here, and just put that in.
Second you do that, our tool appears here.
Don't ask why, this is the easy way when they have a register for it,
but as of this recording, this is the best way.
So we can again ask here, what is the secret word,
and see if it picks it up.
And all depending on how good they are,
they get this wrong, often you need to say, use tools.
So now it found it, and it want us to confirm it.
Hit the breakpoint.
Let me just get rid of the breakpoint, so we don't go back and forward.
We come back, and in the same manner we can ask John,
what the heck is use tools.
So this use tools is all depending on how good
the implementation of the AI is,
and Visual Studio Code is one of the best for tools,
compared to the others, but now it even didn't find that.
So we might want to say...
Oh, that's because they are disabled, that's the reason.
So now it comes and asks, and we get our answer back.
One client I want to show is ChatGBT,
and ChatGBT is hopelessly behind on MCP at the moment,
because they don't support it here.
They have a beta version of it, or preview version,
that we can turn on if you are an admin,
and then we can go in and add a tool,
because so far most MCP clients is developer tools,
and that's a real shame, because it's not all developers who need MCP servers.
So hopefully at some point things like this will become better at it.
I need the URL, so we go back and find that here.
We say MCP, I have no authentication on this,
and I understand it's dangerous.
You will see that ChatGBT is endlessly slow at doing this,
and this beta version has been there for quite a while now,
so I don't know why they are so slow at doing this.
But what we can do is we can go in and take our MCP server,
and again, what is the secret word?
And I know for a fact I need to say use tools here.
So hopefully we should hit our tool.
This is very hit and miss if it works,
but some of the tools can call, and some of them will also fail.
But they called the tool, and now it failed.
Let's try again.
So there's a reason why it's in beta.
Now it appears to want to call it two times for some reason.
Yes, they don't really have the act to get on MCP yet,
but hopefully they will at some point.
But as you can see, MCP is very easy.
Write your tools,
write these three lines of code, and this one,
and you are more or less done.
If you want to see videos about local tools, I will also make that,
but to me, honestly, local tools really don't exist.
I don't think they are worth doing, especially not in a .NET space,
but it is possible, and if you had something
like a command line tool, you might do it,
but my recommendation would be always to do remote tools,
and as an example, ChatGPT, when they get better,
they will only work with remote tools, not local tools,
because they are a remote system.
But that is everything for this time. See you in the next one.