-
Notifications
You must be signed in to change notification settings - Fork 186
Open
Description
I'd like to contribute content to the course that demonstrates how to create an MCP server with CORS enabled using the modern Streamable HTTP transport. This would help learners understand how to:
Set up a compliant MCP server that accepts requests across origins
Configure proper CORS headers to enable frontend communication
Implement http-streamable with real working examples
Align the course with MCP’s updated best practices after SSE deprecation
I already have working code examples and would love to contribute or collaborate to enhance this section of the curriculum.
Let me know if there’s interest or guidance on where this addition might best fit!
Example: server.py
from mcp.server.fastmcp import FastMCP
from starlette.middleware.cors import CORSMiddleware
import random
# Create an MCP server
mcp = FastMCP(
"Weather Service",
stateless_http=True,
)
# Tool implementation
@mcp.tool()
def get_weather(location: str) -> str:
"""Get the current weather for a specified location."""
return f"Weather in {location}: Sunny, 72°F"
@mcp.tool()
def greet(name: str) -> str:
"""Greet a user by name."""
return f"Hello, {name}!"
@mcp.tool()
def add_numbers(a: float, b: float) -> float:
"""Add two numbers together."""
return a + b
jokes = [
"Why do programmers prefer dark mode? Because light attracts bugs.",
"How many programmers does it take to change a light bulb? None, it's a hardware problem.",
"Why was the computer cold? It left its Windows open.",
"Bugs come in through open Windows."
]
@mcp.tool()
def get_random_joke() -> str:
"""Tell a random programming joke."""
return random.choice(jokes)
programming_facts = [
"The first computer programmer was Ada Lovelace.",
"Python is named after the British comedy group Monty Python.",
"The first ever computer virus was created in 1971 and was called the 'Creeper' program."
]
@mcp.tool()
def get_programming_fact() -> str:
"""Get a random programming fact."""
return random.choice(programming_facts)
# Resource implementation
@mcp.resource("weather://{location}")
def weather_resource(location: str) -> str:
"""Provide weather data as a resource."""
return f"Weather data for {location}: Sunny, 72°F"
# Prompt implementation
@mcp.prompt()
def weather_report(location: str) -> str:
"""Create a weather report prompt."""
return f"""You are a weather reporter. Weather report for {location}?"""
# Generates the base Starlette/ASGI application
fastmcp_asgi_app = mcp.streamable_http_app()
# Set up the CORSMiddleware
cors_middleware = CORSMiddleware(
app=fastmcp_asgi_app, # Wraps the app generated by mcp
allow_origins=["*"], # Allows any origin. Change '*' to a list of your domains in production.
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# The final app to run is the middleware wrapping the base app
app_with_cors = cors_middleware
# Run the server
if __name__ == "__main__":
import uvicorn
# Run the app wrapped with middleware
uvicorn.run(app_with_cors, host="127.0.0.1", port=8000)
Works with current Python SDK from Antrhopic.
Source Code
Metadata
Metadata
Assignees
Labels
No labels