Get startedGet started for free

Video: Log and progress notifications

1. Video: Log and progress notifications

Next up is Logging and Progress Notifications. These notifications are really easy to set up and greatly improve the user experience around your MCP server. So let me show you once again a quick demonstration. You might recall the app I showed you just a moment ago, this research application, and if I ask Clod to generate a report on archaeology, it is going to make a tool call through that MCP server that we discussed a moment ago. It's going to call specifically the research function. Now it takes a while for this research function to complete, and as it is running, a user is not really gaining any feedback. So as far as the user knows, this tool call might have actually failed, and it might just be in a stalled state or something like that. So it would be really nice if we could give users a little bit more insight into what is going on behind the scenes. To do so, we can make use of logging and notifications. So let me show you what that exact same query would look like if I turn on logging and notifications, which I did when I just refreshed the page right now. So I'm going to ask the same query again, and now I'm going to get the same research tool call, but now I'm going to get a progress meter and some log statements to go along with it. The progress and the log meter is not fake data. These are log and progress statements that are being emitted inside of the tool call inside of the research tool. So let me show you a little bit of code and help you understand how this works. To get started with logging and progress notifications inside of a tool function on our server, we'll receive the context argument, which is automatically included as the last argument to our tool function. On that context object are a variety of different methods that allow us to either log information or report the progress of this tool run back over to the client. So we get methods like info and report progress. Anytime you call these functions, a message will be automatically sent back to the client. To make use of the log statements and the progress updates on the client, we'll write out a little bit of code similar to what we saw just a moment ago around sampling. so we'll put together a callback function that'll be called whenever we receive a logging statement from the server. We'll also make a separate callback that will receive updates on progress from the server. We'll then take the logging callback and pass it off to our client session and the progress callback and that gets passed off to the call tool function. Inside of these callbacks, it is up to you on how you're going to report these log statements and the progress to your user. You can just print them out at the terminal if you're making a CLI application. If you're making a web app, you'll have to come up with a little bit more clever solution to get that information down to the browser. You, of course, also do not have to actually include any of these log statements or progress statements. You don't have to show them to the user at all if you do not want to. These are just user experience enhancements, once again, to give your user a better idea of what is going on.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.