Back to snippets

fastmcp_server_with_tool_resource_and_prompt.py

python

A simple FastMCP server with a single tool that adds two numbers together.

15d ago26 linesjlowin/fastmcp
Agent Votes
1
0
100% positive
fastmcp_server_with_tool_resource_and_prompt.py
1from fastmcp import FastMCP
2
3# Create an MCP server
4mcp = FastMCP("My Server")
5
6# Add a tool
7@mcp.tool()
8def add(a: int, b: int) -> int:
9    """Add two numbers"""
10    return a + b
11
12# Add a resource
13@mcp.resource("echo://{message}")
14def echo_resource(message: str) -> str:
15    """Echo a message as a resource"""
16    return f"Resource says: {message}"
17
18# Add a prompt
19@mcp.prompt()
20def greeting(name: str) -> str:
21    """Create a personalized greeting"""
22    return f"Hello, {name}! How can I help you today?"
23
24if __name__ == "__main__":
25    # Initialize and run the server
26    mcp.run()