تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Installing MCP and Boost0:00

We've built AI features inside of our app, but now it's time to expose at least one of them to external AI agents safely. If AI is one of your products, then other AI agents will want to connect to it. And MCP is a clean way to expose capabilities without building a custom API for every use case. So the first thing that we want to do is install Laravel MCP. We're going to also install Laravel Boost. Now MCP is for exposing your app's AI features to external agents. So we are

Boost. Now MCP is for exposing your app's AI features to external agents. So we are essentially going to set up an MCP server. We are going to expose whatever agents that we want to expose, and everything is going to be fine. Now Boost, on the other hand, is for improving developer experience when you or your team uses AI internally. So really, for this episode, Boost isn't necessary. In fact, we're really not going to use Boost at all, but it is a feature.

Creating MCP Server1:04

Boost isn't necessary. In fact, we're really not going to use Boost at all, but it is a feature. It's a tool that is part of Laravel. And if you use AI agents, it's going to make your life easier. So next, we want to make an MCP server called support desk server. I mean, really, we could call it whatever we want, but this is going to be fine. Now this is going to serve as the entry point for external agents to connect to. And so the next thing that we need to create

Building Triage Tool1:29

the entry point for external agents to connect to. And so the next thing that we need to create is the tool that we want to expose. So we'll use artisan to make an MCP tool. And this is going to wrap around our agent. And we are going to expose the ticket triage. Except we are going to call it triage ticket, because why not make it confusing? And let's open up that triage ticket. And we can see that we have a handle method. We also have a schema. Now this is an input schema. So basically,

see that we have a handle method. We also have a schema. Now this is an input schema. So basically, we get to define what this is going to accept, which in our case, it makes a whole lot of sense to accept a ticket ID. So we're going to call it ticket ID. And we are going to use this schema object to specify an integer. And we can also set that we need at least a value of one. So this can't have a value of zero or a negative number, it has to start at one, because IDs are positive.

can't have a value of zero or a negative number, it has to start at one, because IDs are positive. So that's our schema. But then we want to actually handle the request and use our agent. So what we are going to do then is handle this request that we receive. Now this is a special kind of request where you can see that it's Laravel MCP request. This is not a normal HTTP request, but it looks normal, because we can call request validate. And then we can be sure that we have

but it looks normal, because we can call request validate. And then we can be sure that we have a ticket ID, because we can say that it's required. And that it's an integer in that the min value is one. But of course, we need to do that using the correct syntax. So we are getting data from the request, we want to ensure that we have an actual ticket ID. So that's the first thing we need to do is use that to get the ticket that we are going to triage. So we will get that ticket

need to do is use that to get the ticket that we are going to triage. So we will get that ticket with find or fail, passing in the data ticket ID. And then we want the response from using the ticket triager, let's make that so that we can prompt that. I don't remember what we prompt, let's open up the ticket triage controller, so that whenever we prompt here, we 're just going to lift this because why not? So our prompt is going to include the subject as well as some of the

lift this because why not? So our prompt is going to include the subject as well as some of the messages. So that's what we want. And then we want to return structured data. And we do that using a response. So just like the request is a special kind of request, the response is a special kind of response. They both live in the Laravel MCP. So we want to return a structured response. And we are going to include the priority, which we get from the response of, well, these are

And we are going to include the priority, which we get from the response of, well, these are yeah, their attributes. So right or a T. And then we want the others, we want department, sentiment, any tags that we might have. And of course, the summary. Now we might not have a summary. So let's let that be no, let's do some copying and pasting. So notice we aren't returning the response that we got from our agent, we are essentially translating that into an MCP structured

the response that we got from our agent, we are essentially translating that into an MCP structured response. And so now that we have our tool, well, that response, return response. Now it doesn't like this, because the structured method returns a response factory and handle wants to return a response. So let's look at this. If we call a method, we have response, we have responses, I guess we could do that. And then first, we'll try it. I mean, the easy way to make

I guess we could do that. And then first, we'll try it. I mean, the easy way to make visual studio code like this is to just get rid of the response type hint for the return type. Not sure. So I know that that works. Let's try this so that we will have responses. And then first, I mean, that should work. We've created the factory with a response. So we are returning the first response. I don't know, we'll see. So we are finished with our triage ticket tool. I want to

Registering Tool and Route5:52

response. I don't know, we'll see. So we are finished with our triage ticket tool. I want to say ticket triage. So now we need to register this with our server. So we want to open up the support desk server that we created. And here we can see the protected array of tools. And this is all that we have to do here, we just need to specify our triage ticket tool. And that's it. And so now that we have our tool that wraps around the agent, which is being exposed by

And so now that we have our tool that wraps around the agent, which is being exposed by our MCP server, we need to register a route to our server. And while we could technically do that from inside of our web routes, instead, what I want to do is break this out into another file, we'll just call it MCP dot PHP. So let's open that up, create the new file. And then we want to set our route. So we will use MCP web for our URL. I don't know. Let's just do MCP. It really doesn't

our route. So we will use MCP web for our URL. I don't know. Let's just do MCP. It really doesn't matter, because we could use whatever we want, really, as long as it doesn't interfere with any of the other routes. MCP, I guess, is going to be fine. And then we just need to specify the class for our support desk server. And that's it. Of course, we need to also pull in the MCP facade. But with that in place, that sets up the route. So now all we have to do is save our route file,

Testing with MCP Inspector7:14

But with that in place, that sets up the route. So now all we have to do is save our route file, our web route file. And let's just close everything to clean things up. Then from the command line, we're going to run PHP artisan MCP inspector MCP. Now we're calling MCP here, we're going to run this. And this is going to start up another web server. And it's going to prompt us to open up the inspector in a browser. So let's pick one. So here's the MCP inspector. This is going to make it easy to

in a browser. So let's pick one. So here's the MCP inspector. This is going to make it easy to be sure that our MCP is exposing our tool and that our agent works okay. So we can see that the URL is automatically populated here. Connection type proxy or direct proxy ought to be fine. So if we click on connect, we get an error and we can't read that error. But the great thing about this is that these errors are also in the console. So we can take a look and see that right here,

this is that these errors are also in the console. So we can take a look and see that right here, the CSRF token mismatch. So one of the ways that we can fix that, and maybe it 's the only way to fix that is to open up bootstrap app dot PHP. And right here in our middleware, where we have our in for AI budgets, we're going to add validate CSRF tokens, except for MCP. And that should fix that. Now I'm not exactly sure if we need to restart the inspector, but we're going to give

that. Now I'm not exactly sure if we need to restart the inspector, but we're going to give this a shot. We can close that we can try to connect again. And there we go, we are connected now. So at the top, we have resources, prompts, tools, we are interested in tools. And we went to list the tools, because hopefully our triage ticket will be here. And we can see that there's a description of what this tool does, which is something that we could control. If we go back to

a description of what this tool does, which is something that we could control. If we go back to the triage ticket, we could add public function description, and then we would return the description for the tool. Let's not worry about that since we are already past that spot. But we'll come in and out just so that we know that it's there. So back inside of the inspector, we can click on triage ticket, and then we can interact with that tool. So we have the ticket ID, if we put in zero, then

ticket, and then we can interact with that tool. So we have the ticket ID, if we put in zero, then we are going to get a message that says that we have to put in a minimum value of one. And here we can see the response, the ticket ID field must be at least one. Okay, fine. So let's clear this out. We'll put one, we'll run the tool, and then we can review the call. So we have the call to our ticket triage, or rather our triage ticket tool, the arguments was the ticket ID of one. And if we

ticket triage, or rather our triage ticket tool, the arguments was the ticket ID of one. And if we look at the response, we can see that, well, the AI says that it's priority one , department billing, sentiment, negative tags, duplicate charge, refund request, and so on and so forth. So there we go, we have just exposed our agent so that external agents could use that if they wanted to. It's great. Again, though, we would want to protect that with sanctum, but that just compl icates things for a

Configuring Boost Guidelines10:21

Again, though, we would want to protect that with sanctum, but that just compl icates things for a demo. So we'll just leave it as is. Now the last thing that I want to show you is boost. We can install boost. Of course, we have to require it with composer first, but then we can install boost, which is going to give us some options. So which boost features would you like to configure guidelines, skills or MCP, we can select all of them if we want. But let's just go with guidelines

guidelines, skills or MCP, we can select all of them if we want. But let's just go with guidelines because guidelines are an important thing. They set up the rules that AI agents are supposed to follow. So which third party AI guidelines skills would you like to install, a layervel AI ought to be fine. And then which agents would you like to configure? So I typically use codecs first, followed by cloud code. So let's use both of those. So let's open up explore, because we now have

followed by cloud code. So let's use both of those. So let's open up explore, because we now have this agents dot MD, and we can see that there are rules layervel boost guidelines, the layervel boost guidelines, blah, blah, blah, blah, foundation context. So if you aren't familiar with using AI agents, basically what you want to do is set up a document, usually it's marked down, and you want to provide a context for your agents, you want to provide guidelines so that the agents

and you want to provide a context for your agents, you want to provide guidelines so that the agents will understand what they aren't supposed to do, or at least what they are supposed to do. And based upon the context, they can make some decent changes, or they could just really screw things up, depending upon, you know, this document. So this is a document that's very important when using an agent. So somewhere here, we are going to see type declarations, and there it is right

using an agent. So somewhere here, we are going to see type declarations, and there it is right there. So always use explicit return type declarations for methods and functions, use appropriate PHP type hints for method parameters, and then, you know, even gives an example. So while it's not necessarily, you know, a tool that you would use to make your lives better, it 's actually a document that other tools will use to make your life better. And again, if you haven't gotten into the world

that other tools will use to make your life better. And again, if you haven't gotten into the world of using AI agents, I highly recommend that you do so. It has certainly changed the way that I write software, as well as everybody else.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟