mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-11-29 06:21:26 +01:00
Adds a workflow that drives this checkout's FastMCP through the latest releases of two major consumers, modeled on uv's ecosystem tests. pydantic-ai's MCPToolset is built on fastmcp.Client, so it runs in the project environment and connects in-process, over stdio, and over streamable HTTP. langchain-mcp-adapters pins mcp<2, so it cannot share an environment with FastMCP 4; it runs in its own environment and reaches the server over stdio and HTTP, covering handshake-era clients. Each path runs an agent loop with a scripted model, direct tool calls, structured output, tool errors, elicitation, progress, logging, resources, resource templates, and prompts. Claude-Session: https://claude.ai/code/session_01KfHgVhbYEhBCC5eSeqGiuG Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
"""The FastMCP server every downstream smoke script connects to.
|
|
|
|
Run as `python server.py stdio` or `python server.py http <port>`.
|
|
"""
|
|
|
|
import sys
|
|
from dataclasses import dataclass
|
|
|
|
from mcp.types import (
|
|
ElicitRequest,
|
|
ElicitRequestFormParams,
|
|
ElicitResult,
|
|
InputRequiredResult,
|
|
)
|
|
from mcp_types.version import MODERN_PROTOCOL_VERSIONS
|
|
from pydantic import BaseModel
|
|
|
|
from fastmcp import Context, FastMCP
|
|
from fastmcp.exceptions import ToolError
|
|
|
|
mcp = FastMCP(
|
|
"downstream-smoke", instructions="Arithmetic and weather for smoke tests."
|
|
)
|
|
|
|
|
|
class Forecast(BaseModel):
|
|
city: str
|
|
celsius: float
|
|
conditions: list[str]
|
|
|
|
|
|
@mcp.tool
|
|
def add(a: int, b: int) -> int:
|
|
"""Add two integers."""
|
|
return a + b
|
|
|
|
|
|
@mcp.tool
|
|
def forecast(city: str, days: int = 1) -> Forecast:
|
|
"""Structured weather forecast for a city."""
|
|
return Forecast(city=city, celsius=21.5, conditions=["sunny"] * days)
|
|
|
|
|
|
@mcp.tool
|
|
def divide(a: float, b: float) -> float:
|
|
"""Divide a by b."""
|
|
if b == 0:
|
|
raise ToolError("cannot divide by zero")
|
|
return a / b
|
|
|
|
|
|
@mcp.tool
|
|
async def count_to(n: int, ctx: Context) -> str:
|
|
"""Count to n, reporting progress and logging along the way."""
|
|
for i in range(1, n + 1):
|
|
await ctx.report_progress(i, n)
|
|
await ctx.info(f"counted to {n}")
|
|
return f"counted to {n}"
|
|
|
|
|
|
@dataclass
|
|
class Approval:
|
|
approved: bool
|
|
|
|
|
|
@mcp.tool
|
|
async def confirm(action: str, ctx: Context) -> str | InputRequiredResult:
|
|
"""Ask the client to approve an action, using whichever elicitation path the connection's protocol era supports."""
|
|
rc = ctx.request_context
|
|
if rc is not None and rc.protocol_version in MODERN_PROTOCOL_VERSIONS:
|
|
if ctx.input_responses is None:
|
|
form = ElicitRequestFormParams(
|
|
message=f"approve {action}?",
|
|
requested_schema={
|
|
"type": "object",
|
|
"properties": {"approved": {"type": "boolean"}},
|
|
"required": ["approved"],
|
|
},
|
|
)
|
|
return InputRequiredResult(
|
|
result_type="input_required",
|
|
input_requests={
|
|
"approval": ElicitRequest(method="elicitation/create", params=form)
|
|
},
|
|
)
|
|
answer = ctx.input_responses["approval"]
|
|
assert isinstance(answer, ElicitResult), answer
|
|
if answer.action == "accept" and answer.content and answer.content["approved"]:
|
|
return f"{action}: approved"
|
|
return f"{action}: {answer.action}"
|
|
result = await ctx.elicit(f"approve {action}?", response_type=Approval)
|
|
if result.action == "accept" and result.data.approved:
|
|
return f"{action}: approved"
|
|
return f"{action}: {result.action}"
|
|
|
|
|
|
@mcp.resource("config://app", mime_type="application/json")
|
|
def app_config() -> str:
|
|
return '{"mode": "smoke"}'
|
|
|
|
|
|
@mcp.resource("greeting://{name}")
|
|
def greeting(name: str) -> str:
|
|
return f"hello, {name}"
|
|
|
|
|
|
@mcp.prompt
|
|
def review(code: str) -> str:
|
|
"""Ask for a code review."""
|
|
return f"please review:\n{code}"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if sys.argv[1:2] == ["http"]:
|
|
mcp.run("http", host="127.0.0.1", port=int(sys.argv[2]), show_banner=False)
|
|
else:
|
|
mcp.run("stdio", show_banner=False)
|