mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 13:34:17 +02:00
109 lines
No EOL
4.5 KiB
Text
109 lines
No EOL
4.5 KiB
Text
---
|
|
title: Proxying Servers
|
|
sidebarTitle: Proxying
|
|
description: Use FastMCP to act as an intermediary or change transport for other MCP servers.
|
|
icon: arrows-retweet
|
|
---
|
|
|
|
FastMCP provides a powerful proxying capability that allows one FastMCP server instance to act as a frontend for another MCP server (which could be remote, running on a different transport, or even another FastMCP instance). This is achieved using the `FastMCP.as_proxy()` class method.
|
|
|
|
## What is Proxying?
|
|
|
|
Proxying means setting up a FastMCP server that doesn't implement its own tools or resources directly. Instead, when it receives a request (like `tools/call` or `resources/read`), it forwards that request to a *backend* MCP server, receives the response, and then relays that response back to the original client.
|
|
|
|
```mermaid
|
|
sequenceDiagram
|
|
participant Client
|
|
participant ProxyServer as FastMCP Proxy Server
|
|
participant BackendServer as Backend MCP Server
|
|
|
|
Client->>ProxyServer: Request (e.g., stdio)
|
|
ProxyServer->>BackendServer: Request (e.g., sse)
|
|
BackendServer-->>ProxyServer: Response (e.g., sse)
|
|
ProxyServer-->>Client: Response (e.g., stdio)
|
|
```
|
|
|
|
### Use Cases
|
|
|
|
- **Transport Bridging**: Expose a server running on one transport (e.g., a remote SSE server) via a different transport (e.g., local Stdio for Claude Desktop).
|
|
- **Adding Functionality**: Insert a layer in front of an existing server to add caching, logging, authentication, or modify requests/responses (though direct modification requires subclassing `FastMCPProxy`).
|
|
- **Security Boundary**: Use the proxy as a controlled gateway to an internal server.
|
|
- **Simplifying Client Configuration**: Provide a single, stable endpoint (the proxy) even if the backend server's location or transport changes.
|
|
|
|
## Creating a Proxy
|
|
|
|
The easiest way to create a proxy is using the `FastMCP.as_proxy()` class method. This creates a standard FastMCP server that forwards requests to another MCP server.
|
|
|
|
```python
|
|
from fastmcp import FastMCP, Client
|
|
|
|
# Create a client configured to talk to the backend server
|
|
# This could be any MCP server - remote, local, or using any transport
|
|
backend_client = Client("backend_server.py") # Could be "http://remote.server/sse", etc.
|
|
|
|
# Create the proxy server with as_proxy()
|
|
proxy_server = await FastMCP.as_proxy(
|
|
backend_client,
|
|
name="MyProxyServer" # Optional settings for the proxy
|
|
)
|
|
|
|
# That's it! You now have a proxy FastMCP server that can be used
|
|
# with any transport (SSE, stdio, etc.) just like any other FastMCP server
|
|
```
|
|
|
|
**How `as_proxy` Works:**
|
|
|
|
1. It connects to the backend server using the provided client.
|
|
2. It discovers all the tools, resources, resource templates, and prompts available on the backend server.
|
|
3. It creates corresponding "proxy" components that forward requests to the backend.
|
|
4. It returns a standard `FastMCP` server instance that can be used like any other.
|
|
|
|
### Bridging Transports
|
|
|
|
A common use case is to bridge transports. For example, making a remote SSE server available locally via Stdio:
|
|
|
|
```python
|
|
from fastmcp import FastMCP, Client
|
|
|
|
# Client targeting a remote SSE server
|
|
client = Client("http://example.com/mcp/sse")
|
|
|
|
# Create a proxy server - it's just a regular FastMCP server
|
|
proxy = await FastMCP.as_proxy(client, name="SSE to Stdio Proxy")
|
|
|
|
# The proxy can now be used with any transport
|
|
# No special handling needed - it works like any FastMCP server
|
|
```
|
|
|
|
### In-Memory Proxies
|
|
|
|
You can also proxy an in-memory `FastMCP` instance, which is useful for adjusting the configuration or behavior of a server you don't completely control.
|
|
|
|
```python
|
|
from fastmcp import FastMCP
|
|
|
|
# Original server
|
|
original_server = FastMCP(name="Original")
|
|
|
|
@original_server.tool()
|
|
def tool_a() -> str:
|
|
return "A"
|
|
|
|
# Create a proxy of the original server
|
|
proxy = await FastMCP.as_proxy(
|
|
original_server,
|
|
name="Proxy Server"
|
|
)
|
|
|
|
# proxy is now a regular FastMCP server that forwards
|
|
# requests to original_server
|
|
```
|
|
|
|
## `FastMCPProxy` Class
|
|
|
|
Internally, `FastMCP.as_proxy()` uses the `FastMCPProxy` class. You generally don't need to interact with this class directly, but it's available if needed. It has two primary async constructors:
|
|
|
|
* `FastMCPProxy.from_client(client: Client, **settings)`: Creates a proxy from a client instance.
|
|
* `FastMCPProxy.from_server(server: FastMCP, **settings)`: Creates a proxy from another FastMCP server instance.
|
|
|
|
Using the class directly might be necessary for advanced scenarios, like subclassing `FastMCPProxy` to add custom logic before or after forwarding requests. |