- Home
- MCP servers
- Sample
Sample
- python
0
GitHub Stars
python
Language
7 months ago
First Indexed
3 months ago
Catalog Refreshed
Documentation & install
Readme and setup notes from the catalogue, plus a client-ready config you can copy for your MCP host.
Installation
Add the following to your MCP client configuration file.
Configuration
View docs{
"mcpServers": {
"tdu-naifen-sample-mcp": {
"command": "python",
"args": [
"server.py"
]
}
}
}The MCP (Model Context Protocol) server lets you expose data, tools, and templates to large language models in a modular way. By building a Python MCP server, you create reusable components that your LLMs can query, execute, and compose to perform tasks with context from external sources.
How to use
You will run the MCP server locally and connect a client (such as the built‑in inspector) to test and use the available components. Start the server in stdio mode so a client can interact by sending requests and receiving responses. Use the inspector to exercise tools, resources, and prompts, and verify that the tool executes, resources return data, and prompts generate correct templates.
How to install
Prerequisites you need to meet before installing and running the server are listed here so you can prepare your environment.
Install Python 3.7 or newer (Python 3.11+ recommended) and the pip package manager.
Install the MCP command line tools using pip.
pip install "mcp[cli]"
Optionally, manage and run projects with uv to streamline initialization and dependency handling.
uv init mcp-server
cd mcp-server
uv add "mcp[cli]"
Create the server file that defines the MCP server components. You can start with the following foundation to implement a simple calculation tool, a dynamic resource, and a prompt template.
# server.py
from mcp.server.fastmcp import FastMCP
# Initialize the MCP server
mcp = FastMCP("Demo Server")
# Define a calculation tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""
Performs addition of two integers.
Args:
a: First number
b: Second number
Returns:
Sum of both numbers
"""
return a + b
# Create a dynamic resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""
Generates a personalized greeting.
Args:
name: Person's name for the greeting
Returns:
Formatted greeting message
"""
return f"Hello, {name}!"
# Add a prompt template
@mcp.prompt()
def review_code(code: str) -> str:
"""
Creates a code review template.
Args:
code: Source code to review
Returns:
Formatted review prompt
"""
return f"Please review this code:\n\n{code}"
# Server execution
if __name__ == "__main__":
mcp.run()
If you use uv for project management, you can keep the server’s dependencies and commands organized as shown above. The server is defined in code and can be run with the next step.
Additional setup and testing
Test the server using the built‑in development inspector. The recommended pathway is to launch the inspector in development mode, connect via stdio, and interact with the Tools, Resources, and Prompts you defined.
mcp dev server.py
In the Inspector, configure the transport to STDIO and point to your server script so you can test interactively.
Transport Type: STDIO
Command: python
Arguments: server.py
Testing each component once connected:
-
Tool testing: Open Tools, select add, provide inputs a and b (for example, 10 and 15), and verify the result is 25.
-
Resource testing: Open Resources, choose get_greeting, enter a name (for example, Alice), and verify the response is Hello, Alice!.
-
Prompt testing: Open Prompts, choose review_code, input sample code (for example, print(1+1)), and observe the generated review prompt output.
Understanding server behavior
When you run the server directly, it waits for a client connection via the stdio transport. This is expected behavior while you are testing with the Inspector or another client.
Available tools
add
Performs addition of two integers as a calculative tool that can be invoked by the LLM.