Guide Published: 6 min read

MCP Servers: Python Tools and Resources

Build a Python MCP server that can manage tasks and guide AI actions.

MCP Servers: Python Tools and Resources

Understanding Model Context Protocol

Model Context Protocol (MCP) gives AI apps a shared way to connect with outside tools and data. It can link a host app to services such as task lists, databases, or APIs. This guide shows how to create an MCP server in Python with FastMCP.

An MCP setup has three main parts. The host is the AI app that a person uses. An MCP client runs within the host and handles messages to a server. The server shares tools, resources, or prompts with that client.

This split lets an AI app use new services without building each connection from scratch. A server sets the features and data it will expose. The host chooses when to call those features. The MCP architecture overview explains how these parts fit together.

Start with one narrow task, such as managing a to-do list. You can use the same pattern for an API, but add the right checks before sharing real data. A small build makes each part easy to test.

Components of an MCP Server

Three connected MCP components shown as glowing modules on a dark indigo background
Connected parts of an MCP server

An MCP server can offer tools, resources, and prompts. Each has a clear job. Tools let the model take action. Resources let it read data. Prompts give it a repeatable way to handle a request.

For a task manager, a tool might add a task or mark it done. A resource might show all open tasks without changing them. A prompt could help turn a broad request into a short, ordered task plan.

Keep read and write actions separate. This makes it clear when a request can change data. It also helps you test what the model can access. Limit each tool to one useful task.

When people ask, “how do I create an MCP server,” the first step is to pick what it should safely expose. A server for an API follows the same idea. Define the actions first, then connect each one to the right API call.

Setting Up Your Development Environment

Layered dark glass planes and a violet module evoke a local Python development setup
A dark, modular Python workspace

Install Python 3.10 or later. Make a project folder and open a terminal in that folder. A virtual environment keeps this project's packages apart from other Python work.

On macOS or Linux, run python -m venv .venv, then source .venv/bin/activate. On Windows, run python -m venv .venv, then .venv\Scripts\activate. Install FastMCP with pip install fastmcp.

FastMCP is a Python framework that makes it easier to define MCP features. The official Python SDK project shares setup notes and examples. Check its current guidance if you need a specific version.

Create a file named server.py. This is a simple way to create an MCP server locally and test it on your own machine. In this first version, tasks will stay in memory and disappear when the process stops.

Building a Simple MCP Server

A compact server block connects with rounded data modules in a dark violet scene
A simple server with linked data modules

Import FastMCP, make a server, and set up a list for task records. Each task can hold an ID, a title, and a done flag. A counter can give each new task a distinct ID.

Put these lines near the top of server.py: from fastmcp import FastMCP, mcp = FastMCP("Task Manager"), tasks = [], and next_id = 1. Each line sets up a part of the small server.

Add mcp.run() at the end of the file. Start it from the active environment with python server.py. The way you connect it depends on your AI host, so use that host's MCP setup steps.

This is how to build a simple MCP server for a first test. It is not a lasting data store. For a real service, save tasks in a database or call an API. Add access checks before exposing private data.

Creating Tools for MCP

Tools let a client ask the server to do something. In FastMCP, add the @mcp.tool decorator above a Python function. A task manager can offer create, read, update, and delete actions, known as CRUD.

For example, an add_task function can take a title, make a record, and add it to the list. Give the new record an ID and set its done flag to false. Return the record so the caller can report what changed.

A list_tasks tool can return all records. An update_task tool can change a title or mark a task done. A delete_task tool can remove a record. Keep each tool's name and result clear.

Check inputs before changing data. Reject blank titles and IDs that do not exist. A complete_task tool can set the done flag to true, then return a short result. If no task matches, return a clear error.

These steps show how to create your own MCP server and how to create an MCP server from scratch with a small feature set. For an API, each tool can call one endpoint instead of changing a local list. Do not let the model send unrestricted requests.

Implementing Resources for MCP

Resources give the model a way to read information without changing it. Add a resource for the current task list, or make one that shows only open tasks. Use the resource for context that the model needs before it acts.

Keep resource output focused. A list of tasks might include each ID, title, and done state. Avoid returning private fields the model does not need. If the data comes from an API, fetch only the fields needed for the task.

Tools and resources can share the same data, but they should not do the same job. A resource reads. A tool changes. This split makes it easier to see what a client can do and to check its behavior.

Defining Prompts for AI Interactions

Prompts provide reusable guidance for common tasks. A task planning prompt could ask the model to sort open tasks by urgency, then suggest a short plan. It should guide the model, not perform a hidden update.

Give a prompt clear inputs and a useful output shape. For example, it might take a goal and the current task list. It can then ask for a small set of next steps. The model can use a tool only when a person asks it to make a change.

Test the server through an MCP client in your chosen host. Check that each tool gets the right input and returns the expected result. Check that resources only read data, and that prompts give useful, structured guidance.

To create an MCP client, you usually configure an MCP-enabled host to connect to your server; the exact steps vary by host. A client sends requests and receives results. Start with harmless test tasks, then add persistent storage, access checks, and API links as the use case grows.

Frequently asked questions

How do you create an MCP server in Python?
Install Python 3.10 or later and FastMCP. Define a server, add tools or resources, then run it with your chosen MCP host.
Can I create a simple MCP server locally?
Yes. Make a Python project, install FastMCP in a virtual environment, and run the server on your machine. A basic example can store data in memory.
What is the difference between an MCP tool and a resource?
A tool lets a model request an action, such as adding a task. A resource lets it read data without changing it.
How can I create an MCP server for an API?
Make a server tool for each API action the model needs. Check inputs, restrict access, and return only the data needed for the task.
How do I create an MCP client?
Use an MCP-enabled host and configure it to connect to your server. Setup steps vary by host, so follow its current instructions.
  • model context protocol
  • Python MCP server
  • FastMCP framework
  • MCP server tools
  • MCP server resources

Keep reading