|
| 1 | +""" |
| 2 | +MiniMCP math server for integration tests. |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import anyio |
| 8 | +from pydantic import Field |
| 9 | + |
| 10 | +from mcp.server.minimcp import MiniMCP |
| 11 | + |
| 12 | +MATH_CONSTANTS = { |
| 13 | + "pi": 3.14159265359, |
| 14 | + "𝜋": 3.14159265359, |
| 15 | + "e": 2.71828182846, |
| 16 | + "𝚎": 2.71828182846, |
| 17 | + "golden_ratio": 1.61803398875, |
| 18 | + "𝚽": 1.61803398875, |
| 19 | + "sqrt_2": 1.41421356237, |
| 20 | + "√2": 1.41421356237, |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +# Create a simple math server for testing directly in this file |
| 25 | +math_mcp = MiniMCP[Any]( |
| 26 | + name="TestMathServer", |
| 27 | + version="0.1.0", |
| 28 | + instructions="A simple MCP server for mathematical operations used in integration tests.", |
| 29 | + include_stack_trace=True, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +# -- Tools -- |
| 34 | +@math_mcp.tool() |
| 35 | +def add(a: float = Field(description="The first number"), b: float = Field(description="The second number")) -> float: |
| 36 | + """Add two numbers""" |
| 37 | + return a + b |
| 38 | + |
| 39 | + |
| 40 | +@math_mcp.tool() |
| 41 | +def add_with_const( |
| 42 | + a: float | str = Field(description="The first value"), b: float | str = Field(description="The second value") |
| 43 | +) -> float: |
| 44 | + """Add two values. Values can be numbers or mathematical constants.""" |
| 45 | + if isinstance(a, str): |
| 46 | + a = float(MATH_CONSTANTS[a]) |
| 47 | + if isinstance(b, str): |
| 48 | + b = float(MATH_CONSTANTS[b]) |
| 49 | + return a + b |
| 50 | + |
| 51 | + |
| 52 | +@math_mcp.tool() |
| 53 | +def subtract( |
| 54 | + a: float = Field(description="The first number"), b: float = Field(description="The second number") |
| 55 | +) -> float: |
| 56 | + """Subtract two numbers""" |
| 57 | + return a - b |
| 58 | + |
| 59 | + |
| 60 | +@math_mcp.tool() |
| 61 | +def multiply( |
| 62 | + a: float = Field(description="The first number"), b: float = Field(description="The second number") |
| 63 | +) -> float: |
| 64 | + """Multiply two numbers""" |
| 65 | + return a * b |
| 66 | + |
| 67 | + |
| 68 | +@math_mcp.tool() |
| 69 | +def divide( |
| 70 | + a: float = Field(description="The first number"), b: float = Field(description="The second number") |
| 71 | +) -> float: |
| 72 | + """Divide two numbers""" |
| 73 | + if b == 0: |
| 74 | + raise ValueError("Cannot divide by zero") |
| 75 | + return a / b |
| 76 | + |
| 77 | + |
| 78 | +@math_mcp.tool(description="Add two numbers") |
| 79 | +async def add_with_progress( |
| 80 | + a: float = Field(description="The first float number"), b: float = Field(description="The second float number") |
| 81 | +) -> float: |
| 82 | + responder = math_mcp.context.get_responder() |
| 83 | + await responder.report_progress(0.1, message="Adding numbers") |
| 84 | + await anyio.sleep(0.1) |
| 85 | + await responder.report_progress(0.4, message="Adding numbers") |
| 86 | + await anyio.sleep(0.1) |
| 87 | + await responder.report_progress(0.7, message="Adding numbers") |
| 88 | + await anyio.sleep(0.1) |
| 89 | + return a + b |
| 90 | + |
| 91 | + |
| 92 | +# -- Prompts -- |
| 93 | +@math_mcp.prompt() |
| 94 | +def math_help(operation: str = Field(description="The mathematical operation to get help with")) -> str: |
| 95 | + """Get help with mathematical operations""" |
| 96 | + return f"""You are a helpful math assistant. |
| 97 | +Provide guidance on how to perform the following mathematical operation: {operation} |
| 98 | +
|
| 99 | +Include: |
| 100 | +1. Step-by-step instructions |
| 101 | +2. Example calculations |
| 102 | +3. Common pitfalls to avoid |
| 103 | +""" |
| 104 | + |
| 105 | + |
| 106 | +# -- Resources -- |
| 107 | + |
| 108 | + |
| 109 | +@math_mcp.resource("math://constants") |
| 110 | +def get_math_constants() -> dict[str, float]: |
| 111 | + """Mathematical constants reference""" |
| 112 | + return MATH_CONSTANTS |
| 113 | + |
| 114 | + |
| 115 | +@math_mcp.resource("math://constants/{constant_name}") |
| 116 | +def get_math_constant(constant_name: str) -> float: |
| 117 | + """Get a specific mathematical constant""" |
| 118 | + if constant_name not in MATH_CONSTANTS: |
| 119 | + raise ValueError(f"Unknown constant: {constant_name}") |
| 120 | + return MATH_CONSTANTS[constant_name] |
0 commit comments