Exploring MCP Primitives: Tools, Resources, and Prompts
Introduction And Lesson Overview
Welcome back! In the previous lesson, you learned what the Model Context Protocol (MCP) is, why it is important, and how to launch a basic MCP server in Python using the FastMCP class. You also explored the two main ways to run your server — using standard input/output (stdio) for local communication and Server-Sent Events (SSE) for networked scenarios. By now, you should feel comfortable starting an MCP server and understanding how it fits into the broader AI integration landscape.
In this lesson, we will build on that foundation by exploring how to define and expose your server’s capabilities. Specifically, you will learn how to add tools, resources, and prompts to your MCP server. These are the core building blocks that allow your server to provide useful functions, share data, and guide AI interactions. By the end of this lesson, you will know how to define each of these primitives and how to interact with them from a client. This will prepare you to create more powerful and interactive MCP servers.
Understanding MCP Server Primitives
To make your MCP server truly useful, you need to expose its capabilities in a way that clients (such as AI agents or other applications) can discover and use. MCP provides three main primitives for this purpose: tools, resources, and prompts.
- Tools are executable functions that perform actions or computations. For example, a tool might add two numbers, fetch data from an API, or process a file. Tools are the “actions” your server can perform on request.
- Resources are data entities that your server exposes. These can be static (like a greeting message or configuration) or dynamic (like user profiles or database records). Resources provide context or information that clients can read and use.
- Prompts are reusable templates that guide AI interactions. They help structure the way an AI model asks questions, explains concepts, or interacts with users. Prompts can accept arguments and generate dynamic messages.
Understanding these primitives is essential because they are the standard way for AI models and clients to discover what your server can do. When you define tools, resources, and prompts, you make your server’s capabilities visible and accessible to the outside world.
Defining MCP Tools
Let’s start by defining a tool. In MCP, a tool is simply a Python function that you expose to clients using a decorator. The FastMCP library makes this easy with the @mcp.tool() decorator. When you decorate a function as a tool, it becomes discoverable and callable by clients.
Here is an example of a simple tool that adds two integers and returns the result:
In this code, the add function takes two integer arguments, a and b, and returns their sum. The @mcp.tool() decorator registers this function as a tool with the MCP server. You can add as many tools as you like, each with its own logic and documentation.
When a client connects to your server, it can list all available tools and see their names, descriptions, and input parameters. This makes it easy for clients to discover what actions your server can perform.
