Overview
Streamable HTTP transport provides a flexible way to connect to remote MCP servers. It’s often built upon HTTP and can support various communication patterns, including request-response and streaming, sometimes utilizing Server-Sent Events (SSE) for server-to-client streams within a broader HTTP interaction.Key Concepts
- Remote Servers: Designed for MCP servers hosted remotely.
- Flexibility: Can support more complex interaction patterns than plain SSE, potentially including bi-directional communication if the server implements it.
MCPServerAdapter
Configuration: You’ll need to provide the server’s base URL for MCP communication and specify"streamable-http"
as the transport type.
Connecting via Streamable HTTP
You have two primary methods for managing the connection lifecycle with a Streamable HTTP MCP server:1. Fully Managed Connection (Recommended)
The recommended approach is to use a Python context manager (with
statement), which handles the connection’s setup and teardown automatically.
"http://localhost:8001/mcp"
with the actual URL of your Streamable HTTP MCP server.
2. Manual Connection Lifecycle
For scenarios requiring more explicit control, you can manage theMCPServerAdapter
connection manually.
It is critical to call
mcp_server_adapter.stop()
when you are done to close the connection and free up resources. A try...finally
block is the safest way to ensure this.Security Considerations
When using Streamable HTTP transport, general web security best practices are paramount:- Use HTTPS: Always prefer HTTPS (HTTP Secure) for your MCP server URLs to encrypt data in transit.
- Authentication: Implement robust authentication mechanisms if your MCP server exposes sensitive tools or data.
- Input Validation: Ensure your MCP server validates all incoming requests and parameters.