Resources in MCP Servers
1. Resources in MCP Servers
Welcome back!2. What are MCP Resources?
We've spent a lot of time so far talking about tools in MCP servers, but that's just one primitive type. In this chapter, we'll look at the other two primitive types: resources and prompts.3. What are MCP Resources?
Let's start with resources. Resources are read-only data items or structured data objects that are retrieved by the MCP client on demand. Resources can be text files, database schemas, database records, or API responses. Unlike tools, resources are not necessarily called by the LLM when it decides it needs to take an action. Resources are either controlled by the application itself, such as loading a user's preferences from their profile, or by a user action, typically through a file upload interface.4. MCP Resources in AI Apps
An AI chat app might connect to an MCP server to retrieve the user's name and summaries from previous conversations to give them a personalized experience. Let's look at how we can add resources to the Currency Converter server we created in the last chapter.5. Defining MCP Server Resources
The good news is that defining MCP resources is very similar to defining tools. To start, we instantiate an MCP server with the FastMCP class; then, we can begin adding resources with the @mcp.resource decorator. Included in this is the Uniform Resource Identifier, or URI, of the resource that we would like to use. These URIs can point to local files in the filesystem, which we will use in this course, as well as APIs and remote servers. They can also be dynamically defined to include certain parameters like user IDs to retrieve private resources. Here, we have a local file called locations.txt available from the server process, containing a list of valid locations that we can convert the timezones for. Next, we define a function for the resource, which reads and returns the file's contents if it's found, and fails gracefully if not. Like with the MCP tools we defined,6. Defining MCP Server Resources
we should also use type hints and a descriptive docstring.7. Saving the Server: timezone_server.py
As with previous MCP servers, we'll now assume this server is saved in a local file called timezone_server.py, which we can connect to with a client through the stdio transport. These resources can be added alongside the tool functions we created before in the same script.8. Client: Listing Resources
Let's create a list_resources() function in our client, so we can list the resources and their descriptions available in the server. The good news is that the bulk of the code is the same as before! We define our server parameters, then open and initialize a session with the async-with and await keywords. With the session open, we can list the resources using the .list_resources() method. This operates exactly the same as the .list_tools() method from Chapter 1, allowing us to extract information about the available resources. We can loop over response.resources and extract the URI, name, and description using their respective attributes, and return the resources list.9. Client: Listing Resources
And there's the result! We get nicely formatted printouts along with a Resource object we can extract information from downstream. Let's now extract this resource's information.10. Client: Reading Resources
We'll define a second function called read_resource() that reads a resource's contents from a given URI. We start with our same boilerplate code for creating a session for the server, then call the .read_resource() method, which is analogous to the .call_tool() method for tools. This reads the resource at the given URI, and from it, we're able to access its contents like the file format, and text content.11. Client: Reading Resources
Running the function, we can see the locations in the file, along with the resource object.12. Let's practice!
Time to try this out!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.