AI Workflows

How to Build an MCP Server with FastMCP in Python

Learn step-by-step how to build and run an MCP server using the FastMCP framework in Python.

5 min read

Learn step-by-step how to build and run a Model Context Protocol (MCP) server using the FastMCP framework in Python. This guide provides developers with the tools and techniques required to create and use their own MCP-based solutions effectively.

Overview of MCP Protocol and FastMCP

The Model Context Protocol (MCP) is a standardized protocol designed to simplify interactions between applications and services. Its primary goal is to establish a common way for AI models (clients) to interact with external services (MCP servers). MCP is widely used to integrate tools, prompts, and resources for agents and other applications.

FastMCP is a popular Python framework based on the official MCP SDK. It utilizes Python-centric features like decorators, type annotations, and asynchronous functionality, making Python MCP server development both efficient and straightforward.


Prerequisites for Building an MCP Server

Before starting to build your FastMCP-based MCP server, ensure the following:

prerequisites

  • Basic knowledge of Python and AI workflows.
  • Python 3.7+ installed on your machine.
  • The FastMCP framework installed via pip install fastmcp.
  • Access to a local development environment (e.g., VS Code).
  • Familiarity with writing functions and working with decorators in Python.

Step-by-Step Guide to Setting up an MCP Server

Follow these steps to create a basic MCP server using FastMCP:

1. Install FastMCP

FastMCP is available via the Python Package Index (PyPI). Use pip to install it:

Install FastMCP

pip install fastmcp
# Verify installation
pip show fastmcp
# Output indicates version, dependencies, etc.

2. Create the Python Script

Create a Python script (e.g., server.py) and include the following code:

python
from fastmcp.server import MCPServer, tool

# Create an instance of an MCP server
server = MCPServer()

# Define a tool using a decorator
@tool(server)
def add_numbers(a: int, b: int) -> int:
    """Add two numbers and return the result."""
    return a + b

# Run the server
if __name__ == "__main__":
    server.run()

3. Set Up an mcp.json Configuration File

In the root directory of your project, create an mcp.json file:

json
{
  "version": "1.0",
  "servers": [
    {
      "id": "basic_mcp_server",
      "name": "Basic MCP Server",
      "description": "A sample MCP server that adds two numbers.",
      "command": "python server.py",
      "type": "stdio"
    }
  ]
}

4. Run the MCP Server in VS Code

Ensure your environment is correctly configured:

steps

  1. Open your project folder in VS Code.
  2. (Optional) Install the GitHub Copilot Labs extension in VS Code for an intuitive MCP experience.
  3. Use the VS Code MCP extension to load and manage your mcp.json file.
  4. Launch your server via the MCP Server section in the VS Code activity bar.
  5. Open Copilot Chat and enable your server via the Tools menu.

5. Test Your MCP Server

Use Copilot Chat or another MCP-supporting tool to test the functionality of your server's tool. For example:

  • Query in Copilot Chat: "What is 5 plus 3?"
  • Output in Response: "The result is 8."

Verifying MCP Server Functionality

To ensure your server works as expected, you can:


Tips for Debugging and Monitoring MCP Servers

Efficient debugging and monitoring are essential for developing robust MCP servers.


Using AI Agents with MCP Servers

MCP servers are often designed to work with AI agents as part of workflows. Here's an example of integrating FastMCP servers with agents:

Using LangChain to Connect an MCP Server

Install LangChain:

Install LangChain

pip install langchain
# Verify installation
pip show langchain
# Output indicates version, dependencies, etc.

Sample Python code:

python
from langchain.agents import initialize_agent, Tool
from langchain.chat_models import ChatOpenAI
from langchain.mcp_tools import MCPTool

# Configure your MCP server details
mcp_server_url = "http://localhost:8000/mcp"

# Define MCP tools
mcp_tool = MCPTool.from_server(url=mcp_server_url)

# Initialize an AI agent with the MCP tool
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
tools = [Tool(name=mcp_tool.name, func=mcp_tool.run)]

agent = initialize_agent(tools, llm, agent="conversational", verbose=True)

# Test the agent
response = agent.run("Add 5 and 10 using the tool.")
print(response)

Deploying MCP Servers into Production

Building a production-ready MCP server requires additional steps to ensure security, scalability, and reliability.



FAQ

What is FastMCP used for?

FastMCP is used to build MCP servers in Python. It simplifies the process of creating Model Context Protocol (MCP) servers by providing decorators, type annotations, and other Pythonic features.

Can FastMCP integrate with AI agents?

Yes, FastMCP-based servers can integrate seamlessly with AI agent frameworks such as LangChain and Microsoft's Agent Framework. These enable agents to use the tools and services exposed by the MCP server as part of their workflows.

Is FastMCP suitable for production deployments?

Yes, FastMCP supports production-ready deployments. For example, servers can be hosted using the HTTP protocol and secured with authentication. Additionally, observability tools such as OpenTelemetry can be integrated to monitor production servers effectively.

What is MCP Inspector?

MCP Inspector is a UI tool for directly testing and interacting with MCP servers without relying on an intermediary agent such as Copilot. It provides a way to inspect server tools, resources, and logs.