fastmcp 2.10.1__py3-none-any.whl → 2.10.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastmcp/cli/cli.py +117 -225
- fastmcp/cli/install/__init__.py +20 -0
- fastmcp/cli/install/claude_code.py +186 -0
- fastmcp/cli/install/claude_desktop.py +186 -0
- fastmcp/cli/install/cursor.py +196 -0
- fastmcp/cli/install/mcp_config.py +165 -0
- fastmcp/cli/install/shared.py +85 -0
- fastmcp/cli/run.py +17 -4
- fastmcp/client/client.py +230 -124
- fastmcp/client/elicitation.py +5 -0
- fastmcp/client/transports.py +3 -5
- fastmcp/mcp_config.py +282 -0
- fastmcp/prompts/prompt.py +2 -4
- fastmcp/resources/resource.py +2 -2
- fastmcp/resources/template.py +1 -1
- fastmcp/server/auth/providers/bearer.py +15 -6
- fastmcp/server/openapi.py +40 -9
- fastmcp/server/proxy.py +206 -37
- fastmcp/server/server.py +102 -20
- fastmcp/settings.py +19 -1
- fastmcp/tools/tool.py +3 -2
- fastmcp/tools/tool_transform.py +5 -6
- fastmcp/utilities/cli.py +102 -0
- fastmcp/utilities/json_schema.py +14 -3
- fastmcp/utilities/logging.py +3 -0
- fastmcp/utilities/openapi.py +92 -0
- fastmcp/utilities/tests.py +13 -0
- fastmcp/utilities/types.py +4 -3
- {fastmcp-2.10.1.dist-info → fastmcp-2.10.3.dist-info}/METADATA +4 -3
- {fastmcp-2.10.1.dist-info → fastmcp-2.10.3.dist-info}/RECORD +33 -26
- fastmcp/utilities/mcp_config.py +0 -93
- {fastmcp-2.10.1.dist-info → fastmcp-2.10.3.dist-info}/WHEEL +0 -0
- {fastmcp-2.10.1.dist-info → fastmcp-2.10.3.dist-info}/entry_points.txt +0 -0
- {fastmcp-2.10.1.dist-info → fastmcp-2.10.3.dist-info}/licenses/LICENSE +0 -0
fastmcp/server/proxy.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from pathlib import Path
|
|
3
6
|
from typing import TYPE_CHECKING, Any, cast
|
|
4
7
|
from urllib.parse import quote
|
|
5
8
|
|
|
6
9
|
import mcp.types
|
|
10
|
+
from mcp.client.session import ClientSession
|
|
11
|
+
from mcp.shared.context import LifespanContextT, RequestContext
|
|
7
12
|
from mcp.shared.exceptions import McpError
|
|
8
13
|
from mcp.types import (
|
|
9
14
|
METHOD_NOT_FOUND,
|
|
@@ -13,14 +18,21 @@ from mcp.types import (
|
|
|
13
18
|
)
|
|
14
19
|
from pydantic.networks import AnyUrl
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
import fastmcp
|
|
22
|
+
from fastmcp.client.client import Client, FastMCP1Server
|
|
23
|
+
from fastmcp.client.elicitation import ElicitResult
|
|
24
|
+
from fastmcp.client.logging import LogMessage
|
|
25
|
+
from fastmcp.client.roots import RootsList
|
|
26
|
+
from fastmcp.client.transports import ClientTransportT
|
|
17
27
|
from fastmcp.exceptions import NotFoundError, ResourceError, ToolError
|
|
28
|
+
from fastmcp.mcp_config import MCPConfig
|
|
18
29
|
from fastmcp.prompts import Prompt, PromptMessage
|
|
19
30
|
from fastmcp.prompts.prompt import PromptArgument
|
|
20
31
|
from fastmcp.prompts.prompt_manager import PromptManager
|
|
21
32
|
from fastmcp.resources import Resource, ResourceTemplate
|
|
22
33
|
from fastmcp.resources.resource_manager import ResourceManager
|
|
23
34
|
from fastmcp.server.context import Context
|
|
35
|
+
from fastmcp.server.dependencies import get_context
|
|
24
36
|
from fastmcp.server.server import FastMCP
|
|
25
37
|
from fastmcp.tools.tool import Tool, ToolResult
|
|
26
38
|
from fastmcp.tools.tool_manager import ToolManager
|
|
@@ -35,9 +47,9 @@ logger = get_logger(__name__)
|
|
|
35
47
|
class ProxyToolManager(ToolManager):
|
|
36
48
|
"""A ToolManager that sources its tools from a remote client in addition to local and mounted tools."""
|
|
37
49
|
|
|
38
|
-
def __init__(self,
|
|
50
|
+
def __init__(self, client_factory: Callable[[], Client], **kwargs):
|
|
39
51
|
super().__init__(**kwargs)
|
|
40
|
-
self.
|
|
52
|
+
self.client_factory = client_factory
|
|
41
53
|
|
|
42
54
|
async def get_tools(self) -> dict[str, Tool]:
|
|
43
55
|
"""Gets the unfiltered tool inventory including local, mounted, and proxy tools."""
|
|
@@ -46,13 +58,12 @@ class ProxyToolManager(ToolManager):
|
|
|
46
58
|
|
|
47
59
|
# Then add proxy tools, but don't overwrite existing ones
|
|
48
60
|
try:
|
|
49
|
-
|
|
50
|
-
|
|
61
|
+
client = self.client_factory()
|
|
62
|
+
async with client:
|
|
63
|
+
client_tools = await client.list_tools()
|
|
51
64
|
for tool in client_tools:
|
|
52
65
|
if tool.name not in all_tools:
|
|
53
|
-
all_tools[tool.name] = ProxyTool.from_mcp_tool(
|
|
54
|
-
self.client, tool
|
|
55
|
-
)
|
|
66
|
+
all_tools[tool.name] = ProxyTool.from_mcp_tool(client, tool)
|
|
56
67
|
except McpError as e:
|
|
57
68
|
if e.error.code == METHOD_NOT_FOUND:
|
|
58
69
|
pass # No tools available from proxy
|
|
@@ -73,8 +84,9 @@ class ProxyToolManager(ToolManager):
|
|
|
73
84
|
return await super().call_tool(key, arguments)
|
|
74
85
|
except NotFoundError:
|
|
75
86
|
# If not found locally, try proxy
|
|
76
|
-
|
|
77
|
-
|
|
87
|
+
client = self.client_factory()
|
|
88
|
+
async with client:
|
|
89
|
+
result = await client.call_tool(key, arguments)
|
|
78
90
|
return ToolResult(
|
|
79
91
|
content=result.content,
|
|
80
92
|
structured_content=result.structured_content,
|
|
@@ -84,9 +96,9 @@ class ProxyToolManager(ToolManager):
|
|
|
84
96
|
class ProxyResourceManager(ResourceManager):
|
|
85
97
|
"""A ResourceManager that sources its resources from a remote client in addition to local and mounted resources."""
|
|
86
98
|
|
|
87
|
-
def __init__(self,
|
|
99
|
+
def __init__(self, client_factory: Callable[[], Client], **kwargs):
|
|
88
100
|
super().__init__(**kwargs)
|
|
89
|
-
self.
|
|
101
|
+
self.client_factory = client_factory
|
|
90
102
|
|
|
91
103
|
async def get_resources(self) -> dict[str, Resource]:
|
|
92
104
|
"""Gets the unfiltered resource inventory including local, mounted, and proxy resources."""
|
|
@@ -95,12 +107,13 @@ class ProxyResourceManager(ResourceManager):
|
|
|
95
107
|
|
|
96
108
|
# Then add proxy resources, but don't overwrite existing ones
|
|
97
109
|
try:
|
|
98
|
-
|
|
99
|
-
|
|
110
|
+
client = self.client_factory()
|
|
111
|
+
async with client:
|
|
112
|
+
client_resources = await client.list_resources()
|
|
100
113
|
for resource in client_resources:
|
|
101
114
|
if str(resource.uri) not in all_resources:
|
|
102
115
|
all_resources[str(resource.uri)] = (
|
|
103
|
-
ProxyResource.from_mcp_resource(
|
|
116
|
+
ProxyResource.from_mcp_resource(client, resource)
|
|
104
117
|
)
|
|
105
118
|
except McpError as e:
|
|
106
119
|
if e.error.code == METHOD_NOT_FOUND:
|
|
@@ -117,12 +130,13 @@ class ProxyResourceManager(ResourceManager):
|
|
|
117
130
|
|
|
118
131
|
# Then add proxy templates, but don't overwrite existing ones
|
|
119
132
|
try:
|
|
120
|
-
|
|
121
|
-
|
|
133
|
+
client = self.client_factory()
|
|
134
|
+
async with client:
|
|
135
|
+
client_templates = await client.list_resource_templates()
|
|
122
136
|
for template in client_templates:
|
|
123
137
|
if template.uriTemplate not in all_templates:
|
|
124
138
|
all_templates[template.uriTemplate] = (
|
|
125
|
-
ProxyTemplate.from_mcp_template(
|
|
139
|
+
ProxyTemplate.from_mcp_template(client, template)
|
|
126
140
|
)
|
|
127
141
|
except McpError as e:
|
|
128
142
|
if e.error.code == METHOD_NOT_FOUND:
|
|
@@ -149,8 +163,9 @@ class ProxyResourceManager(ResourceManager):
|
|
|
149
163
|
return await super().read_resource(uri)
|
|
150
164
|
except NotFoundError:
|
|
151
165
|
# If not found locally, try proxy
|
|
152
|
-
|
|
153
|
-
|
|
166
|
+
client = self.client_factory()
|
|
167
|
+
async with client:
|
|
168
|
+
result = await client.read_resource(uri)
|
|
154
169
|
if isinstance(result[0], TextResourceContents):
|
|
155
170
|
return result[0].text
|
|
156
171
|
elif isinstance(result[0], BlobResourceContents):
|
|
@@ -162,9 +177,9 @@ class ProxyResourceManager(ResourceManager):
|
|
|
162
177
|
class ProxyPromptManager(PromptManager):
|
|
163
178
|
"""A PromptManager that sources its prompts from a remote client in addition to local and mounted prompts."""
|
|
164
179
|
|
|
165
|
-
def __init__(self,
|
|
180
|
+
def __init__(self, client_factory: Callable[[], Client], **kwargs):
|
|
166
181
|
super().__init__(**kwargs)
|
|
167
|
-
self.
|
|
182
|
+
self.client_factory = client_factory
|
|
168
183
|
|
|
169
184
|
async def get_prompts(self) -> dict[str, Prompt]:
|
|
170
185
|
"""Gets the unfiltered prompt inventory including local, mounted, and proxy prompts."""
|
|
@@ -173,12 +188,13 @@ class ProxyPromptManager(PromptManager):
|
|
|
173
188
|
|
|
174
189
|
# Then add proxy prompts, but don't overwrite existing ones
|
|
175
190
|
try:
|
|
176
|
-
|
|
177
|
-
|
|
191
|
+
client = self.client_factory()
|
|
192
|
+
async with client:
|
|
193
|
+
client_prompts = await client.list_prompts()
|
|
178
194
|
for prompt in client_prompts:
|
|
179
195
|
if prompt.name not in all_prompts:
|
|
180
196
|
all_prompts[prompt.name] = ProxyPrompt.from_mcp_prompt(
|
|
181
|
-
|
|
197
|
+
client, prompt
|
|
182
198
|
)
|
|
183
199
|
except McpError as e:
|
|
184
200
|
if e.error.code == METHOD_NOT_FOUND:
|
|
@@ -204,8 +220,9 @@ class ProxyPromptManager(PromptManager):
|
|
|
204
220
|
return await super().render_prompt(name, arguments)
|
|
205
221
|
except NotFoundError:
|
|
206
222
|
# If not found locally, try proxy
|
|
207
|
-
|
|
208
|
-
|
|
223
|
+
client = self.client_factory()
|
|
224
|
+
async with client:
|
|
225
|
+
result = await client.get_prompt(name, arguments)
|
|
209
226
|
return result
|
|
210
227
|
|
|
211
228
|
|
|
@@ -236,7 +253,6 @@ class ProxyTool(Tool):
|
|
|
236
253
|
context: Context | None = None,
|
|
237
254
|
) -> ToolResult:
|
|
238
255
|
"""Executes the tool by making a call through the client."""
|
|
239
|
-
# This is where the remote execution logic lives.
|
|
240
256
|
async with self._client:
|
|
241
257
|
result = await self._client.call_tool_mcp(
|
|
242
258
|
name=self.name,
|
|
@@ -258,14 +274,22 @@ class ProxyResource(Resource):
|
|
|
258
274
|
_client: Client
|
|
259
275
|
_value: str | bytes | None = None
|
|
260
276
|
|
|
261
|
-
def __init__(
|
|
277
|
+
def __init__(
|
|
278
|
+
self,
|
|
279
|
+
client: Client,
|
|
280
|
+
*,
|
|
281
|
+
_value: str | bytes | None = None,
|
|
282
|
+
**kwargs,
|
|
283
|
+
):
|
|
262
284
|
super().__init__(**kwargs)
|
|
263
285
|
self._client = client
|
|
264
286
|
self._value = _value
|
|
265
287
|
|
|
266
288
|
@classmethod
|
|
267
289
|
def from_mcp_resource(
|
|
268
|
-
cls,
|
|
290
|
+
cls,
|
|
291
|
+
client: Client,
|
|
292
|
+
mcp_resource: mcp.types.Resource,
|
|
269
293
|
) -> ProxyResource:
|
|
270
294
|
"""Factory method to create a ProxyResource from a raw MCP resource schema."""
|
|
271
295
|
return cls(
|
|
@@ -388,21 +412,166 @@ class ProxyPrompt(Prompt):
|
|
|
388
412
|
class FastMCPProxy(FastMCP):
|
|
389
413
|
"""
|
|
390
414
|
A FastMCP server that acts as a proxy to a remote MCP-compliant server.
|
|
391
|
-
It uses specialized managers that fulfill requests via
|
|
415
|
+
It uses specialized managers that fulfill requests via a client factory.
|
|
392
416
|
"""
|
|
393
417
|
|
|
394
|
-
def __init__(
|
|
418
|
+
def __init__(
|
|
419
|
+
self,
|
|
420
|
+
client: Client | None = None,
|
|
421
|
+
*,
|
|
422
|
+
client_factory: Callable[[], Client] | None = None,
|
|
423
|
+
**kwargs,
|
|
424
|
+
):
|
|
395
425
|
"""
|
|
396
426
|
Initializes the proxy server.
|
|
397
427
|
|
|
428
|
+
FastMCPProxy requires explicit session management via client_factory.
|
|
429
|
+
Use FastMCP.as_proxy() for convenience with automatic session strategy.
|
|
430
|
+
|
|
398
431
|
Args:
|
|
399
|
-
client:
|
|
432
|
+
client: [DEPRECATED] A Client instance. Use client_factory instead for explicit
|
|
433
|
+
session management. When provided, a client_factory will be automatically
|
|
434
|
+
created that provides session isolation for backwards compatibility.
|
|
435
|
+
client_factory: A callable that returns a Client instance when called.
|
|
436
|
+
This gives you full control over session creation and reuse.
|
|
400
437
|
**kwargs: Additional settings for the FastMCP server.
|
|
401
438
|
"""
|
|
439
|
+
|
|
402
440
|
super().__init__(**kwargs)
|
|
403
|
-
|
|
441
|
+
|
|
442
|
+
# Handle client and client_factory parameters
|
|
443
|
+
if client is not None and client_factory is not None:
|
|
444
|
+
raise ValueError("Cannot specify both 'client' and 'client_factory'")
|
|
445
|
+
|
|
446
|
+
if client is not None:
|
|
447
|
+
# Deprecated in 2.10.3
|
|
448
|
+
if fastmcp.settings.deprecation_warnings:
|
|
449
|
+
warnings.warn(
|
|
450
|
+
"Passing 'client' to FastMCPProxy is deprecated. Use 'client_factory' instead for explicit session management. "
|
|
451
|
+
"For automatic session strategy, use FastMCP.as_proxy().",
|
|
452
|
+
DeprecationWarning,
|
|
453
|
+
stacklevel=2,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
# Create a factory that provides session isolation for backwards compatibility
|
|
457
|
+
def deprecated_client_factory():
|
|
458
|
+
return client.new()
|
|
459
|
+
|
|
460
|
+
self.client_factory = deprecated_client_factory
|
|
461
|
+
elif client_factory is not None:
|
|
462
|
+
self.client_factory = client_factory
|
|
463
|
+
else:
|
|
464
|
+
raise ValueError("Must specify 'client_factory'")
|
|
404
465
|
|
|
405
466
|
# Replace the default managers with our specialized proxy managers.
|
|
406
|
-
self._tool_manager = ProxyToolManager(
|
|
407
|
-
self._resource_manager = ProxyResourceManager(
|
|
408
|
-
|
|
467
|
+
self._tool_manager = ProxyToolManager(client_factory=self.client_factory)
|
|
468
|
+
self._resource_manager = ProxyResourceManager(
|
|
469
|
+
client_factory=self.client_factory
|
|
470
|
+
)
|
|
471
|
+
self._prompt_manager = ProxyPromptManager(client_factory=self.client_factory)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
async def default_proxy_roots_handler(
|
|
475
|
+
context: RequestContext[ClientSession, LifespanContextT],
|
|
476
|
+
) -> RootsList:
|
|
477
|
+
"""
|
|
478
|
+
A handler that forwards the list roots request from the remote server to the proxy's connected clients and relays the response back to the remote server.
|
|
479
|
+
"""
|
|
480
|
+
ctx = get_context()
|
|
481
|
+
return await ctx.list_roots()
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class ProxyClient(Client[ClientTransportT]):
|
|
485
|
+
"""
|
|
486
|
+
A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients.
|
|
487
|
+
Supports forwarding roots, sampling, elicitation, logging, and progress.
|
|
488
|
+
"""
|
|
489
|
+
|
|
490
|
+
def __init__(
|
|
491
|
+
self,
|
|
492
|
+
transport: ClientTransportT
|
|
493
|
+
| FastMCP
|
|
494
|
+
| FastMCP1Server
|
|
495
|
+
| AnyUrl
|
|
496
|
+
| Path
|
|
497
|
+
| MCPConfig
|
|
498
|
+
| dict[str, Any]
|
|
499
|
+
| str,
|
|
500
|
+
**kwargs,
|
|
501
|
+
):
|
|
502
|
+
if "roots" not in kwargs:
|
|
503
|
+
kwargs["roots"] = default_proxy_roots_handler
|
|
504
|
+
if "sampling_handler" not in kwargs:
|
|
505
|
+
kwargs["sampling_handler"] = ProxyClient.default_sampling_handler
|
|
506
|
+
if "elicitation_handler" not in kwargs:
|
|
507
|
+
kwargs["elicitation_handler"] = ProxyClient.default_elicitation_handler
|
|
508
|
+
if "log_handler" not in kwargs:
|
|
509
|
+
kwargs["log_handler"] = ProxyClient.default_log_handler
|
|
510
|
+
if "progress_handler" not in kwargs:
|
|
511
|
+
kwargs["progress_handler"] = ProxyClient.default_progress_handler
|
|
512
|
+
super().__init__(**kwargs | dict(transport=transport))
|
|
513
|
+
|
|
514
|
+
@classmethod
|
|
515
|
+
async def default_sampling_handler(
|
|
516
|
+
cls,
|
|
517
|
+
messages: list[mcp.types.SamplingMessage],
|
|
518
|
+
params: mcp.types.CreateMessageRequestParams,
|
|
519
|
+
context: RequestContext[ClientSession, LifespanContextT],
|
|
520
|
+
) -> mcp.types.CreateMessageResult:
|
|
521
|
+
"""
|
|
522
|
+
A handler that forwards the sampling request from the remote server to the proxy's connected clients and relays the response back to the remote server.
|
|
523
|
+
"""
|
|
524
|
+
ctx = get_context()
|
|
525
|
+
content = await ctx.sample(
|
|
526
|
+
[msg for msg in messages],
|
|
527
|
+
system_prompt=params.systemPrompt,
|
|
528
|
+
temperature=params.temperature,
|
|
529
|
+
max_tokens=params.maxTokens,
|
|
530
|
+
model_preferences=params.modelPreferences,
|
|
531
|
+
)
|
|
532
|
+
if isinstance(content, mcp.types.ResourceLink | mcp.types.EmbeddedResource):
|
|
533
|
+
raise RuntimeError("Content is not supported")
|
|
534
|
+
return mcp.types.CreateMessageResult(
|
|
535
|
+
role="assistant",
|
|
536
|
+
model="fastmcp-client",
|
|
537
|
+
content=content,
|
|
538
|
+
)
|
|
539
|
+
|
|
540
|
+
@classmethod
|
|
541
|
+
async def default_elicitation_handler(
|
|
542
|
+
cls,
|
|
543
|
+
message: str,
|
|
544
|
+
response_type: type,
|
|
545
|
+
params: mcp.types.ElicitRequestParams,
|
|
546
|
+
context: RequestContext[ClientSession, LifespanContextT],
|
|
547
|
+
) -> ElicitResult:
|
|
548
|
+
"""
|
|
549
|
+
A handler that forwards the elicitation request from the remote server to the proxy's connected clients and relays the response back to the remote server.
|
|
550
|
+
"""
|
|
551
|
+
ctx = get_context()
|
|
552
|
+
result = await ctx.elicit(message, response_type)
|
|
553
|
+
if result.action == "accept":
|
|
554
|
+
return result.data
|
|
555
|
+
else:
|
|
556
|
+
return ElicitResult(action=result.action)
|
|
557
|
+
|
|
558
|
+
@classmethod
|
|
559
|
+
async def default_log_handler(cls, message: LogMessage) -> None:
|
|
560
|
+
"""
|
|
561
|
+
A handler that forwards the log notification from the remote server to the proxy's connected clients.
|
|
562
|
+
"""
|
|
563
|
+
ctx = get_context()
|
|
564
|
+
await ctx.log(message.data, level=message.level, logger_name=message.logger)
|
|
565
|
+
|
|
566
|
+
@classmethod
|
|
567
|
+
async def default_progress_handler(
|
|
568
|
+
cls,
|
|
569
|
+
progress: float,
|
|
570
|
+
total: float | None,
|
|
571
|
+
message: str | None,
|
|
572
|
+
) -> None:
|
|
573
|
+
"""
|
|
574
|
+
A handler that forwards the progress notification from the remote server to the proxy's connected clients.
|
|
575
|
+
"""
|
|
576
|
+
ctx = get_context()
|
|
577
|
+
await ctx.report_progress(progress, total, message)
|
fastmcp/server/server.py
CHANGED
|
@@ -43,6 +43,7 @@ from starlette.routing import BaseRoute, Route
|
|
|
43
43
|
import fastmcp
|
|
44
44
|
import fastmcp.server
|
|
45
45
|
from fastmcp.exceptions import DisabledError, NotFoundError
|
|
46
|
+
from fastmcp.mcp_config import MCPConfig
|
|
46
47
|
from fastmcp.prompts import Prompt, PromptManager
|
|
47
48
|
from fastmcp.prompts.prompt import FunctionPrompt
|
|
48
49
|
from fastmcp.resources import Resource, ResourceManager
|
|
@@ -60,9 +61,9 @@ from fastmcp.settings import Settings
|
|
|
60
61
|
from fastmcp.tools import ToolManager
|
|
61
62
|
from fastmcp.tools.tool import FunctionTool, Tool, ToolResult
|
|
62
63
|
from fastmcp.utilities.cache import TimedCache
|
|
64
|
+
from fastmcp.utilities.cli import log_server_banner
|
|
63
65
|
from fastmcp.utilities.components import FastMCPComponent
|
|
64
66
|
from fastmcp.utilities.logging import get_logger
|
|
65
|
-
from fastmcp.utilities.mcp_config import MCPConfig
|
|
66
67
|
from fastmcp.utilities.types import NotSet, NotSetT
|
|
67
68
|
|
|
68
69
|
if TYPE_CHECKING:
|
|
@@ -285,6 +286,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
285
286
|
async def run_async(
|
|
286
287
|
self,
|
|
287
288
|
transport: Transport | None = None,
|
|
289
|
+
show_banner: bool = True,
|
|
288
290
|
**transport_kwargs: Any,
|
|
289
291
|
) -> None:
|
|
290
292
|
"""Run the FastMCP server asynchronously.
|
|
@@ -298,15 +300,23 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
298
300
|
raise ValueError(f"Unknown transport: {transport}")
|
|
299
301
|
|
|
300
302
|
if transport == "stdio":
|
|
301
|
-
await self.run_stdio_async(
|
|
303
|
+
await self.run_stdio_async(
|
|
304
|
+
show_banner=show_banner,
|
|
305
|
+
**transport_kwargs,
|
|
306
|
+
)
|
|
302
307
|
elif transport in {"http", "sse", "streamable-http"}:
|
|
303
|
-
await self.run_http_async(
|
|
308
|
+
await self.run_http_async(
|
|
309
|
+
transport=transport,
|
|
310
|
+
show_banner=show_banner,
|
|
311
|
+
**transport_kwargs,
|
|
312
|
+
)
|
|
304
313
|
else:
|
|
305
314
|
raise ValueError(f"Unknown transport: {transport}")
|
|
306
315
|
|
|
307
316
|
def run(
|
|
308
317
|
self,
|
|
309
318
|
transport: Transport | None = None,
|
|
319
|
+
show_banner: bool = True,
|
|
310
320
|
**transport_kwargs: Any,
|
|
311
321
|
) -> None:
|
|
312
322
|
"""Run the FastMCP server. Note this is a synchronous function.
|
|
@@ -315,7 +325,14 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
315
325
|
transport: Transport protocol to use ("stdio", "sse", or "streamable-http")
|
|
316
326
|
"""
|
|
317
327
|
|
|
318
|
-
anyio.run(
|
|
328
|
+
anyio.run(
|
|
329
|
+
partial(
|
|
330
|
+
self.run_async,
|
|
331
|
+
transport,
|
|
332
|
+
show_banner=show_banner,
|
|
333
|
+
**transport_kwargs,
|
|
334
|
+
)
|
|
335
|
+
)
|
|
319
336
|
|
|
320
337
|
def _setup_handlers(self) -> None:
|
|
321
338
|
"""Set up core MCP protocol handlers."""
|
|
@@ -1321,8 +1338,16 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1321
1338
|
enabled=enabled,
|
|
1322
1339
|
)
|
|
1323
1340
|
|
|
1324
|
-
async def run_stdio_async(self) -> None:
|
|
1341
|
+
async def run_stdio_async(self, show_banner: bool = True) -> None:
|
|
1325
1342
|
"""Run the server using stdio transport."""
|
|
1343
|
+
|
|
1344
|
+
# Display server banner
|
|
1345
|
+
if show_banner:
|
|
1346
|
+
log_server_banner(
|
|
1347
|
+
server=self,
|
|
1348
|
+
transport="stdio",
|
|
1349
|
+
)
|
|
1350
|
+
|
|
1326
1351
|
async with stdio_server() as (read_stream, write_stream):
|
|
1327
1352
|
logger.info(f"Starting MCP server {self.name!r} with transport 'stdio'")
|
|
1328
1353
|
await self._mcp_server.run(
|
|
@@ -1335,6 +1360,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1335
1360
|
|
|
1336
1361
|
async def run_http_async(
|
|
1337
1362
|
self,
|
|
1363
|
+
show_banner: bool = True,
|
|
1338
1364
|
transport: Literal["http", "streamable-http", "sse"] = "http",
|
|
1339
1365
|
host: str | None = None,
|
|
1340
1366
|
port: int | None = None,
|
|
@@ -1342,6 +1368,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1342
1368
|
path: str | None = None,
|
|
1343
1369
|
uvicorn_config: dict[str, Any] | None = None,
|
|
1344
1370
|
middleware: list[ASGIMiddleware] | None = None,
|
|
1371
|
+
stateless_http: bool | None = None,
|
|
1345
1372
|
) -> None:
|
|
1346
1373
|
"""Run the server using HTTP transport.
|
|
1347
1374
|
|
|
@@ -1352,15 +1379,39 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1352
1379
|
log_level: Log level for the server (defaults to settings.log_level)
|
|
1353
1380
|
path: Path for the endpoint (defaults to settings.streamable_http_path or settings.sse_path)
|
|
1354
1381
|
uvicorn_config: Additional configuration for the Uvicorn server
|
|
1382
|
+
middleware: A list of middleware to apply to the app
|
|
1383
|
+
stateless_http: Whether to use stateless HTTP (defaults to settings.stateless_http)
|
|
1355
1384
|
"""
|
|
1385
|
+
|
|
1356
1386
|
host = host or self._deprecated_settings.host
|
|
1357
1387
|
port = port or self._deprecated_settings.port
|
|
1358
1388
|
default_log_level_to_use = (
|
|
1359
1389
|
log_level or self._deprecated_settings.log_level
|
|
1360
1390
|
).lower()
|
|
1361
1391
|
|
|
1362
|
-
app = self.http_app(
|
|
1392
|
+
app = self.http_app(
|
|
1393
|
+
path=path,
|
|
1394
|
+
transport=transport,
|
|
1395
|
+
middleware=middleware,
|
|
1396
|
+
stateless_http=stateless_http,
|
|
1397
|
+
)
|
|
1398
|
+
|
|
1399
|
+
# Get the path for the server URL
|
|
1400
|
+
server_path = (
|
|
1401
|
+
app.state.path.lstrip("/")
|
|
1402
|
+
if hasattr(app, "state") and hasattr(app.state, "path")
|
|
1403
|
+
else path or ""
|
|
1404
|
+
)
|
|
1363
1405
|
|
|
1406
|
+
# Display server banner
|
|
1407
|
+
if show_banner:
|
|
1408
|
+
log_server_banner(
|
|
1409
|
+
server=self,
|
|
1410
|
+
transport=transport,
|
|
1411
|
+
host=host,
|
|
1412
|
+
port=port,
|
|
1413
|
+
path=server_path,
|
|
1414
|
+
)
|
|
1364
1415
|
_uvicorn_config_from_user = uvicorn_config or {}
|
|
1365
1416
|
|
|
1366
1417
|
config_kwargs: dict[str, Any] = {
|
|
@@ -1378,6 +1429,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1378
1429
|
logger.info(
|
|
1379
1430
|
f"Starting MCP server {self.name!r} with transport {transport!r} on http://{host}:{port}/{path}"
|
|
1380
1431
|
)
|
|
1432
|
+
|
|
1381
1433
|
await server.serve()
|
|
1382
1434
|
|
|
1383
1435
|
async def run_sse_async(
|
|
@@ -1591,9 +1643,8 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1591
1643
|
resource_separator: Deprecated. Separator character for resource URIs.
|
|
1592
1644
|
prompt_separator: Deprecated. Separator character for prompt names.
|
|
1593
1645
|
"""
|
|
1594
|
-
from fastmcp import Client
|
|
1595
1646
|
from fastmcp.client.transports import FastMCPTransport
|
|
1596
|
-
from fastmcp.server.proxy import FastMCPProxy
|
|
1647
|
+
from fastmcp.server.proxy import FastMCPProxy, ProxyClient
|
|
1597
1648
|
|
|
1598
1649
|
# Deprecated since 2.9.0
|
|
1599
1650
|
# Prior to 2.9.0, the first positional argument was the prefix and the
|
|
@@ -1645,7 +1696,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1645
1696
|
as_proxy = server._has_lifespan
|
|
1646
1697
|
|
|
1647
1698
|
if as_proxy and not isinstance(server, FastMCPProxy):
|
|
1648
|
-
server = FastMCPProxy(
|
|
1699
|
+
server = FastMCPProxy(ProxyClient(transport=FastMCPTransport(server)))
|
|
1649
1700
|
|
|
1650
1701
|
# Delegate mounting to all three managers
|
|
1651
1702
|
mounted_server = MountedServer(
|
|
@@ -1856,14 +1907,16 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1856
1907
|
@classmethod
|
|
1857
1908
|
def as_proxy(
|
|
1858
1909
|
cls,
|
|
1859
|
-
backend:
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1910
|
+
backend: (
|
|
1911
|
+
Client[ClientTransportT]
|
|
1912
|
+
| ClientTransport
|
|
1913
|
+
| FastMCP[Any]
|
|
1914
|
+
| AnyUrl
|
|
1915
|
+
| Path
|
|
1916
|
+
| MCPConfig
|
|
1917
|
+
| dict[str, Any]
|
|
1918
|
+
| str
|
|
1919
|
+
),
|
|
1867
1920
|
**settings: Any,
|
|
1868
1921
|
) -> FastMCPProxy:
|
|
1869
1922
|
"""Create a FastMCP proxy server for the given backend.
|
|
@@ -1874,14 +1927,43 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1874
1927
|
`fastmcp.client.Client` constructor.
|
|
1875
1928
|
"""
|
|
1876
1929
|
from fastmcp.client.client import Client
|
|
1877
|
-
from fastmcp.server.proxy import FastMCPProxy
|
|
1930
|
+
from fastmcp.server.proxy import FastMCPProxy, ProxyClient
|
|
1878
1931
|
|
|
1879
1932
|
if isinstance(backend, Client):
|
|
1880
1933
|
client = backend
|
|
1934
|
+
# Session strategy based on client connection state:
|
|
1935
|
+
# - Connected clients: reuse existing session for all requests
|
|
1936
|
+
# - Disconnected clients: create fresh sessions per request for isolation
|
|
1937
|
+
if client.is_connected():
|
|
1938
|
+
from fastmcp.utilities.logging import get_logger
|
|
1939
|
+
|
|
1940
|
+
logger = get_logger(__name__)
|
|
1941
|
+
logger.info(
|
|
1942
|
+
"Proxy detected connected client - reusing existing session for all requests. "
|
|
1943
|
+
"This may cause context mixing in concurrent scenarios."
|
|
1944
|
+
)
|
|
1945
|
+
|
|
1946
|
+
# Reuse sessions - return the same client instance
|
|
1947
|
+
def reuse_client_factory():
|
|
1948
|
+
return client
|
|
1949
|
+
|
|
1950
|
+
client_factory = reuse_client_factory
|
|
1951
|
+
else:
|
|
1952
|
+
# Fresh sessions per request
|
|
1953
|
+
def fresh_client_factory():
|
|
1954
|
+
return client.new()
|
|
1955
|
+
|
|
1956
|
+
client_factory = fresh_client_factory
|
|
1881
1957
|
else:
|
|
1882
|
-
|
|
1958
|
+
base_client = ProxyClient(backend)
|
|
1959
|
+
|
|
1960
|
+
# Fresh client created from transport - use fresh sessions per request
|
|
1961
|
+
def proxy_client_factory():
|
|
1962
|
+
return base_client.new()
|
|
1963
|
+
|
|
1964
|
+
client_factory = proxy_client_factory
|
|
1883
1965
|
|
|
1884
|
-
return FastMCPProxy(
|
|
1966
|
+
return FastMCPProxy(client_factory=client_factory, **settings)
|
|
1885
1967
|
|
|
1886
1968
|
@classmethod
|
|
1887
1969
|
def from_client(
|
fastmcp/settings.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations as _annotations
|
|
2
2
|
|
|
3
3
|
import inspect
|
|
4
|
+
import warnings
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from typing import Annotated, Any, Literal
|
|
6
7
|
|
|
@@ -258,4 +259,21 @@ class Settings(BaseSettings):
|
|
|
258
259
|
] = None
|
|
259
260
|
|
|
260
261
|
|
|
261
|
-
|
|
262
|
+
def __getattr__(name: str):
|
|
263
|
+
"""
|
|
264
|
+
Used to deprecate the module-level Image class; can be removed once it is no longer imported to root.
|
|
265
|
+
"""
|
|
266
|
+
if name == "settings":
|
|
267
|
+
import fastmcp
|
|
268
|
+
|
|
269
|
+
settings = fastmcp.settings
|
|
270
|
+
# Deprecated in 2.10.2
|
|
271
|
+
if settings.deprecation_warnings:
|
|
272
|
+
warnings.warn(
|
|
273
|
+
"`from fastmcp.settings import settings` is deprecated. use `fasmtpc.settings` instead.",
|
|
274
|
+
DeprecationWarning,
|
|
275
|
+
stacklevel=2,
|
|
276
|
+
)
|
|
277
|
+
return settings
|
|
278
|
+
|
|
279
|
+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
|
fastmcp/tools/tool.py
CHANGED
|
@@ -46,7 +46,7 @@ class _UnserializableType:
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
def default_serializer(data: Any) -> str:
|
|
49
|
-
return pydantic_core.to_json(data, fallback=str
|
|
49
|
+
return pydantic_core.to_json(data, fallback=str).decode()
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
class ToolResult:
|
|
@@ -434,6 +434,7 @@ def _convert_to_content(
|
|
|
434
434
|
_process_as_single_item: bool = False,
|
|
435
435
|
) -> list[ContentBlock]:
|
|
436
436
|
"""Convert a result to a sequence of content objects."""
|
|
437
|
+
|
|
437
438
|
if result is None:
|
|
438
439
|
return []
|
|
439
440
|
|
|
@@ -467,7 +468,7 @@ def _convert_to_content(
|
|
|
467
468
|
|
|
468
469
|
if other_content:
|
|
469
470
|
other_content = _convert_to_content(
|
|
470
|
-
other_content
|
|
471
|
+
other_content,
|
|
471
472
|
serializer=serializer,
|
|
472
473
|
_process_as_single_item=True,
|
|
473
474
|
)
|