mirror of
https://github.com/github/awesome-copilot.git
synced 2026-10-13 07:14:24 +00:00
1.9 KiB
1.9 KiB
Streamable HTTP Servers
Use this transport only for a remote server. A local server uses stdio.
Run the server
Replace mcp.run() in server.py:
import os
if __name__ == "__main__":
mcp.run(
transport="streamable-http",
host=os.environ.get("HOST", "127.0.0.1"),
port=int(os.environ.get("PORT", "8000")),
)
Clients connect to http://HOST:PORT/mcp. For VS Code, write .vscode/mcp.json:
{
"servers": {
"demo": {
"type": "http",
"url": "http://localhost:8000/mcp"
}
}
}
Mount the server in an ASGI app
- Use
mcp.streamable_http_app()to mount the server in a Starlette or FastAPI app. - Give the transport options to
streamable_http_app(). This method has noportoption. - Make the lifespan of the host app enter
mcp.session_manager.run().
Gotchas
- Requests on the 2026-07-28 revision have no session. Any replica can answer a request.
stateless_http=Truechanges only how the server serves clients of earlier revisions.json_response=Truesends one JSON body for each request. In this mode, the server cannot send progress notifications during the request.- If you do not set
transport_security, the SDK checks theHostandOriginheaders only when the host is127.0.0.1,localhost, or::1. For all other hosts, for example0.0.0.0or a public host name, the server does no check. For these hosts, givetransport_security=TransportSecuritySettings(allowed_hosts=[...], allowed_origins=[...])(frommcp.server.transport_security) torun()orstreamable_http_app(). - For browser clients, add CORS to the host app. Allow the
Mcp-*request headers. Expose theMcp-Session-Idresponse header. - For more than one worker, set
request_state_security=RequestStateSecurity(keys=[...])(frommcp.server.mcpserver) onMCPServer. Use the same keys and the same server name on all workers.