README - replace code snippets with examples - direct execution and display utilities (#1137)
This commit is contained in:
parent
9bf5160eea
commit
a10fec7fa6
2 changed files with 254 additions and 8 deletions
27
examples/snippets/servers/direct_execution.py
Normal file
27
examples/snippets/servers/direct_execution.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
"""Example showing direct execution of an MCP server.
|
||||
|
||||
This is the simplest way to run an MCP server directly.
|
||||
cd to the `examples/snippets` directory and run:
|
||||
uv run direct-execution-server
|
||||
or
|
||||
python servers/direct_execution.py
|
||||
"""
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
mcp = FastMCP("My App")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def hello(name: str = "World") -> str:
|
||||
"""Say hello to someone."""
|
||||
return f"Hello, {name}!"
|
||||
|
||||
|
||||
def main():
|
||||
"""Entry point for the direct execution server."""
|
||||
mcp.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
"""Example showing structured output with tools."""
|
||||
|
||||
from typing import TypedDict
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
|
@ -27,3 +29,69 @@ def get_weather(city: str) -> WeatherData:
|
|||
condition="sunny",
|
||||
wind_speed=5.2,
|
||||
)
|
||||
|
||||
|
||||
# Using TypedDict for simpler structures
|
||||
class LocationInfo(TypedDict):
|
||||
latitude: float
|
||||
longitude: float
|
||||
name: str
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def get_location(address: str) -> LocationInfo:
|
||||
"""Get location coordinates"""
|
||||
return LocationInfo(latitude=51.5074, longitude=-0.1278, name="London, UK")
|
||||
|
||||
|
||||
# Using dict[str, Any] for flexible schemas
|
||||
@mcp.tool()
|
||||
def get_statistics(data_type: str) -> dict[str, float]:
|
||||
"""Get various statistics"""
|
||||
return {"mean": 42.5, "median": 40.0, "std_dev": 5.2}
|
||||
|
||||
|
||||
# Ordinary classes with type hints work for structured output
|
||||
class UserProfile:
|
||||
name: str
|
||||
age: int
|
||||
email: str | None = None
|
||||
|
||||
def __init__(self, name: str, age: int, email: str | None = None):
|
||||
self.name = name
|
||||
self.age = age
|
||||
self.email = email
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def get_user(user_id: str) -> UserProfile:
|
||||
"""Get user profile - returns structured data"""
|
||||
return UserProfile(name="Alice", age=30, email="alice@example.com")
|
||||
|
||||
|
||||
# Classes WITHOUT type hints cannot be used for structured output
|
||||
class UntypedConfig:
|
||||
def __init__(self, setting1, setting2):
|
||||
self.setting1 = setting1
|
||||
self.setting2 = setting2
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def get_config() -> UntypedConfig:
|
||||
"""This returns unstructured output - no schema generated"""
|
||||
return UntypedConfig("value1", "value2")
|
||||
|
||||
|
||||
# Lists and other types are wrapped automatically
|
||||
@mcp.tool()
|
||||
def list_cities() -> list[str]:
|
||||
"""Get a list of cities"""
|
||||
return ["London", "Paris", "Tokyo"]
|
||||
# Returns: {"result": ["London", "Paris", "Tokyo"]}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def get_temperature(city: str) -> float:
|
||||
"""Get temperature as a simple float"""
|
||||
return 22.5
|
||||
# Returns: {"result": 22.5}
|
||||
|
|
|
|||
Loading…
Reference in a new issue