docs: Update examples to use stateless HTTP with JSON responses (#1499)
Some checks failed
Main branch checks / checks (push) Has been cancelled
Some checks failed
Main branch checks / checks (push) Has been cancelled
This commit is contained in:
parent
9c8f763aa8
commit
fcffa14b5b
8 changed files with 76 additions and 38 deletions
|
|
@ -1,14 +1,14 @@
|
|||
"""
|
||||
FastMCP quickstart example.
|
||||
|
||||
cd to the `examples/snippets/clients` directory and run:
|
||||
uv run server fastmcp_quickstart stdio
|
||||
Run from the repository root:
|
||||
uv run examples/snippets/servers/fastmcp_quickstart.py
|
||||
"""
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create an MCP server
|
||||
mcp = FastMCP("Demo")
|
||||
mcp = FastMCP("Demo", json_response=True)
|
||||
|
||||
|
||||
# Add an addition tool
|
||||
|
|
@ -36,3 +36,8 @@ def greet_user(name: str, style: str = "friendly") -> str:
|
|||
}
|
||||
|
||||
return f"{styles.get(style, styles['friendly'])} for someone named {name}."
|
||||
|
||||
|
||||
# Run with streamable HTTP transport
|
||||
if __name__ == "__main__":
|
||||
mcp.run(transport="streamable-http")
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class SimpleTokenVerifier(TokenVerifier):
|
|||
# Create FastMCP instance as a Resource Server
|
||||
mcp = FastMCP(
|
||||
"Weather Service",
|
||||
json_response=True,
|
||||
# Token verifier for authentication
|
||||
token_verifier=SimpleTokenVerifier(),
|
||||
# Auth settings for RFC 9728 Protected Resource Metadata
|
||||
|
|
|
|||
|
|
@ -5,15 +5,15 @@ Run from the repository root:
|
|||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Stateful server (maintains session state)
|
||||
mcp = FastMCP("StatefulServer")
|
||||
# Stateless server with JSON responses (recommended)
|
||||
mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
|
||||
|
||||
# Other configuration options:
|
||||
# Stateless server (no session persistence)
|
||||
# Stateless server with SSE streaming responses
|
||||
# mcp = FastMCP("StatelessServer", stateless_http=True)
|
||||
|
||||
# Stateless server (no session persistence, no sse stream with supported client)
|
||||
# mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
|
||||
# Stateful server with session persistence
|
||||
# mcp = FastMCP("StatefulServer")
|
||||
|
||||
|
||||
# Add a simple tool to demonstrate the server
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from starlette.routing import Mount
|
|||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create MCP server
|
||||
mcp = FastMCP("My App")
|
||||
mcp = FastMCP("My App", json_response=True)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from starlette.routing import Host
|
|||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create MCP server
|
||||
mcp = FastMCP("MCP Host App")
|
||||
mcp = FastMCP("MCP Host App", json_response=True)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ from starlette.routing import Mount
|
|||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create multiple MCP servers
|
||||
api_mcp = FastMCP("API Server")
|
||||
chat_mcp = FastMCP("Chat Server")
|
||||
api_mcp = FastMCP("API Server", json_response=True)
|
||||
chat_mcp = FastMCP("Chat Server", json_response=True)
|
||||
|
||||
|
||||
@api_mcp.tool()
|
||||
|
|
|
|||
|
|
@ -12,7 +12,11 @@ from mcp.server.fastmcp import FastMCP
|
|||
|
||||
# Configure streamable_http_path during initialization
|
||||
# This server will mount at the root of wherever it's mounted
|
||||
mcp_at_root = FastMCP("My Server", streamable_http_path="/")
|
||||
mcp_at_root = FastMCP(
|
||||
"My Server",
|
||||
json_response=True,
|
||||
streamable_http_path="/",
|
||||
)
|
||||
|
||||
|
||||
@mcp_at_root.tool()
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from starlette.routing import Mount
|
|||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create the Echo server
|
||||
echo_mcp = FastMCP(name="EchoServer", stateless_http=True)
|
||||
echo_mcp = FastMCP(name="EchoServer", stateless_http=True, json_response=True)
|
||||
|
||||
|
||||
@echo_mcp.tool()
|
||||
|
|
@ -21,7 +21,7 @@ def echo(message: str) -> str:
|
|||
|
||||
|
||||
# Create the Math server
|
||||
math_mcp = FastMCP(name="MathServer", stateless_http=True)
|
||||
math_mcp = FastMCP(name="MathServer", stateless_http=True, json_response=True)
|
||||
|
||||
|
||||
@math_mcp.tool()
|
||||
|
|
|
|||
Loading…
Reference in a new issue