|
| 1 | +"""Example MCP server with test tools""" |
| 2 | +import time |
| 3 | +from typing import Annotated, Optional, TypedDict, NotRequired |
| 4 | +from zeromcp import McpToolError, McpServer |
| 5 | + |
| 6 | +mcp = McpServer("example") |
| 7 | + |
| 8 | +class SystemInfo(TypedDict): |
| 9 | + platform: Annotated[str, "Operating system platform"] |
| 10 | + python_version: Annotated[str, "Python version"] |
| 11 | + machine: Annotated[str, "Machine architecture"] |
| 12 | + timestamp: Annotated[float, "Current timestamp"] |
| 13 | + |
| 14 | +class GreetingResponse(TypedDict): |
| 15 | + message: Annotated[str, "Greeting message"] |
| 16 | + name: Annotated[str, "Name that was greeted"] |
| 17 | + age: Annotated[NotRequired[int], "Age if provided"] |
| 18 | + |
| 19 | +@mcp.tool |
| 20 | +def divide( |
| 21 | + numerator: Annotated[float, "Numerator"], |
| 22 | + denominator: Annotated[float, "Denominator"] |
| 23 | +) -> float: |
| 24 | + """Divide two numbers (no zero check - tests natural exceptions)""" |
| 25 | + return numerator / denominator |
| 26 | + |
| 27 | +@mcp.tool |
| 28 | +def greet( |
| 29 | + name: Annotated[str, "Name to greet"], |
| 30 | + age: Annotated[Optional[int], "Age of person"] = None |
| 31 | +) -> GreetingResponse: |
| 32 | + """Generate a greeting message""" |
| 33 | + if age is not None: |
| 34 | + return { |
| 35 | + "message": f"Hello, {name}! You are {age} years old.", |
| 36 | + "name": name, |
| 37 | + "age": age |
| 38 | + } |
| 39 | + return { |
| 40 | + "message": f"Hello, {name}!", |
| 41 | + "name": name |
| 42 | + } |
| 43 | + |
| 44 | +@mcp.tool |
| 45 | +def get_system_info() -> SystemInfo: |
| 46 | + """Get system information""" |
| 47 | + import platform |
| 48 | + return { |
| 49 | + "platform": platform.system(), |
| 50 | + "python_version": platform.python_version(), |
| 51 | + "machine": platform.machine(), |
| 52 | + "timestamp": time.time() |
| 53 | + } |
| 54 | + |
| 55 | +@mcp.tool |
| 56 | +def failing_tool(message: Annotated[str, "Error message to raise"]) -> str: |
| 57 | + """Tool that always fails (for testing error handling)""" |
| 58 | + raise McpToolError(message) |
| 59 | + |
| 60 | +class StructInfo(TypedDict): |
| 61 | + name: Annotated[str, "Structure name"] |
| 62 | + size: Annotated[int, "Structure size in bytes"] |
| 63 | + fields: Annotated[list[str], "List of field names"] |
| 64 | + |
| 65 | +@mcp.tool |
| 66 | +def struct_get( |
| 67 | + names: Annotated[list[str], "Array of structure names"] |
| 68 | + | Annotated[str, "Single structure name"] |
| 69 | +) -> list[StructInfo]: |
| 70 | + """Retrieve structure information by names""" |
| 71 | + return [ |
| 72 | + StructInfo({ |
| 73 | + "name": name, |
| 74 | + "size": 128, # Dummy size |
| 75 | + "fields": ["field1", "field2", "field3"] # Dummy fields |
| 76 | + }) |
| 77 | + for name in (names if isinstance(names, list) else [names]) |
| 78 | + ] |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + print("Starting MCP Example Server...") |
| 82 | + print("\nAvailable tools:") |
| 83 | + for name in mcp.tools.methods.keys(): |
| 84 | + func = mcp.tools.methods[name] |
| 85 | + print(f" - {name}: {func.__doc__}") |
| 86 | + |
| 87 | + mcp.start("127.0.0.1", 5001) |
| 88 | + |
| 89 | + print("\n" + "="*60) |
| 90 | + print("Server is running. Press Ctrl+C to stop.") |
| 91 | + print("="*60) |
| 92 | + |
| 93 | + try: |
| 94 | + while True: |
| 95 | + time.sleep(1) |
| 96 | + except KeyboardInterrupt: |
| 97 | + print("\n\nStopping server...") |
| 98 | + mcp.stop() |
| 99 | + print("Server stopped.") |
0 commit comments