zzstoatzz commited on
Commit
6cb3059
·
1 Parent(s): e9ca2f3

update proxy docs + example

Browse files
docs/servers/proxy.mdx CHANGED
@@ -89,7 +89,7 @@ proxy = FastMCP.from_client(client, name="SSE to Stdio Proxy")
89
  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.
90
 
91
  ```python
92
- from fastmcp import FastMCP
93
 
94
  # Original server
95
  original_server = FastMCP(name="Original")
@@ -98,9 +98,12 @@ original_server = FastMCP(name="Original")
98
  def tool_a() -> str:
99
  return "A"
100
 
101
- # Create a proxy of the original server
 
 
 
102
  proxy = FastMCP.from_client(
103
- original_server,
104
  name="Proxy Server"
105
  )
106
 
 
89
  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.
90
 
91
  ```python
92
+ from fastmcp import FastMCP, Client
93
 
94
  # Original server
95
  original_server = FastMCP(name="Original")
 
98
  def tool_a() -> str:
99
  return "A"
100
 
101
+ # To proxy an in-memory server, first create a Client to it.
102
+ client_to_original = Client(original_server)
103
+
104
+ # Create a proxy of the original server using the client.
105
  proxy = FastMCP.from_client(
106
+ client_to_original,
107
  name="Proxy Server"
108
  )
109
 
examples/in_memory_proxy_example.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This example demonstrates how to set up and use an in-memory FastMCP proxy.
3
+
4
+ It illustrates the pattern:
5
+ 1. Create an original FastMCP server with some tools.
6
+ 2. Create a Client that connects to this original server (in-memory).
7
+ 3. Create a proxy FastMCP server using FastMCP.from_client(), passing it the client from step 2.
8
+ 4. Use another Client to connect to the proxy server (in-memory) and interact with the original server's tools through the proxy.
9
+ """
10
+
11
+ import asyncio
12
+
13
+ from mcp.types import TextContent
14
+
15
+ from fastmcp import FastMCP
16
+ from fastmcp.client import Client
17
+
18
+
19
+ class EchoService:
20
+ """A simple service to demonstrate with"""
21
+
22
+ def echo(self, message: str) -> str:
23
+ return f"Original server echoes: {message}"
24
+
25
+
26
+ async def main():
27
+ print("--- In-Memory FastMCP Proxy Example ---")
28
+ print("This example will walk through setting up an in-memory proxy.")
29
+ print("-----------------------------------------")
30
+
31
+ # 1. Original Server Setup
32
+ print(
33
+ "\nStep 1: Setting up the Original Server (OriginalEchoServer) with an 'echo' tool..."
34
+ )
35
+ original_server = FastMCP("OriginalEchoServer")
36
+ original_server.add_tool(EchoService().echo)
37
+ print(f" -> Original Server '{original_server.name}' created.")
38
+
39
+ # 2. Client for Proxy
40
+ print("\nStep 2: Creating a Client to connect to the Original Server...")
41
+ print(" (This client will be used internally by the proxy server)")
42
+ client_to_original = Client(original_server)
43
+ print(f" -> Client for proxy created, targeting '{original_server.name}'.")
44
+
45
+ # 3. Proxy Server Creation
46
+ print("\nStep 3: Creating the Proxy Server (InMemoryProxy)...")
47
+ print(
48
+ f" (Using FastMCP.from_client, passing it the client from Step 2 that targets '{original_server.name}')"
49
+ )
50
+ proxy_server = FastMCP.from_client(client_to_original, name="InMemoryProxy")
51
+ print(
52
+ f" -> Proxy Server '{proxy_server.name}' created, proxying '{original_server.name}'."
53
+ )
54
+
55
+ # 4. Interacting via Proxy
56
+ print("\nStep 4: Using a new Client to connect to the Proxy Server and interact...")
57
+ async with Client(proxy_server) as final_client:
58
+ print(f" -> Successfully connected to proxy '{proxy_server.name}'.")
59
+
60
+ print("\n Listing tools available via proxy...")
61
+ tools = await final_client.list_tools()
62
+ if tools:
63
+ print(" Available Tools:")
64
+ for tool in tools:
65
+ print(
66
+ f" - {tool.name} (Description: {tool.description or 'N/A'})"
67
+ )
68
+ else:
69
+ print(" No tools found via proxy.")
70
+
71
+ message_to_echo = "Hello, simplified proxied world!"
72
+ print(f"\n Calling 'echo' tool via proxy with message: '{message_to_echo}'")
73
+ try:
74
+ result = await final_client.call_tool("echo", {"message": message_to_echo})
75
+ if result and isinstance(result[0], TextContent):
76
+ print(f" Result from proxied 'echo' call: '{result[0].text}'")
77
+ else:
78
+ print(
79
+ f" Error: Unexpected result format from proxied 'echo' call: {result}"
80
+ )
81
+ except Exception as e:
82
+ print(f" Error calling 'echo' tool via proxy: {e}")
83
+
84
+ print("\n-----------------------------------------")
85
+ print("--- In-Memory Proxy Example Finished ---")
86
+
87
+
88
+ if __name__ == "__main__":
89
+ asyncio.run(main())