Example client and server
server.py
from fastmcp import FastMCP# 1. Create the server
mcp = FastMCP(name="My First MCP Server")# 2. Add a tool
@mcp.tool
def add(a: int, b: int) -> int:
"""Adds two integer numbers together."""
return a + b
# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()
Note : use this command to run (python server.py)
client.py
import asyncio
from fastmcp import Client, FastMCP# Local Python script
client = Client("server.py")async def main():
async with client:
# Basic server interaction
await client.ping()
# Execute operations
result = await client.call_tool("add", {"a": 1,"b": 2})
print(result.data)asyncio.run(main())
Note : open new terminal and use this command to run (python client.py)
Resources adalah data atau konten terstruktur yang dapat diakses oleh LLM untuk mendapatkan konteks. Ini bisa berupa dokumen, entri database, atau file konfigurasi.
Example client and server
server.py
from fastmcp import FastMCP# 1. Create the server
mcp = FastMCP(name="My First MCP Server")# 2. Add a static resource
@mcp.resource("resource://config")
def get_config() -> dict:
"""Provides the application's configuration."""
return {"version": "1.0", "author": "MyTeam"}# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()
Note : use this command to run (python server.py)
client.py
import asyncio
from fastmcp import Client# Local Python script
client = Client("server.py")async def main():
async with client:
# Basic server interaction
await client.ping()
result = await client.read_resource("resource://config")
print(result[0].text)asyncio.run(main())
Note : open new terminal and use this command to run (python client.py)
Example client and server
server.py
from fastmcp import FastMCP# 1. Create the server
mcp = FastMCP(name="My First MCP Server")# 2. Add a prompt
@mcp.prompt
def analyze_data(data_points: list[float]) -> str:
"""Creates a prompt asking for analysis of numerical data."""
formatted_data = ", ".join(str(point) for point in data_points)
return f"Please analyze these data points: {formatted_data}"# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()
Note : use this command to run (python server.py)
client.py
import asyncio
from fastmcp import Client# Local Python script
client = Client("server.py")async def main():
async with client:
# Basic server interaction
await client.ping()
result = await client.get_prompt("analyze_data", {
"data_points": [3.2, 4.5, 5.1],
})
print(result.messages[0].content.text)asyncio.run(main())
Note : open new terminal and use this command to run (python client.py)
Kisah Inspiratif dari Malang Bersama n8n.io dan Google Gemini