28
28
class MCPServer (abc .ABC ):
29
29
"""Base class for Model Context Protocol servers."""
30
30
31
+ def __init__ (self , use_structured_content : bool = False ):
32
+ """
33
+ Args:
34
+ use_structured_content: Whether to use `tool_result.structured_content` when calling an
35
+ MCP tool.Defaults to False for backwards compatibility - most MCP servers still
36
+ include the structured content in the `tool_result.content`, and using it by
37
+ default will cause duplicate content. You can set this to True if you know the
38
+ server will not duplicate the structured content in the `tool_result.content`.
39
+ """
40
+ self .use_structured_content = use_structured_content
41
+
31
42
@abc .abstractmethod
32
43
async def connect (self ):
33
44
"""Connect to the server. For example, this might mean spawning a subprocess or
@@ -86,6 +97,7 @@ def __init__(
86
97
cache_tools_list : bool ,
87
98
client_session_timeout_seconds : float | None ,
88
99
tool_filter : ToolFilter = None ,
100
+ use_structured_content : bool = False ,
89
101
):
90
102
"""
91
103
Args:
@@ -98,7 +110,13 @@ def __init__(
98
110
99
111
client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
100
112
tool_filter: The tool filter to use for filtering tools.
113
+ use_structured_content: Whether to use `tool_result.structured_content` when calling an
114
+ MCP tool. Defaults to False for backwards compatibility - most MCP servers still
115
+ include the structured content in the `tool_result.content`, and using it by
116
+ default will cause duplicate content. You can set this to True if you know the
117
+ server will not duplicate the structured content in the `tool_result.content`.
101
118
"""
119
+ super ().__init__ (use_structured_content = use_structured_content )
102
120
self .session : ClientSession | None = None
103
121
self .exit_stack : AsyncExitStack = AsyncExitStack ()
104
122
self ._cleanup_lock : asyncio .Lock = asyncio .Lock ()
@@ -346,6 +364,7 @@ def __init__(
346
364
name : str | None = None ,
347
365
client_session_timeout_seconds : float | None = 5 ,
348
366
tool_filter : ToolFilter = None ,
367
+ use_structured_content : bool = False ,
349
368
):
350
369
"""Create a new MCP server based on the stdio transport.
351
370
@@ -364,11 +383,17 @@ def __init__(
364
383
command.
365
384
client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
366
385
tool_filter: The tool filter to use for filtering tools.
386
+ use_structured_content: Whether to use `tool_result.structured_content` when calling an
387
+ MCP tool. Defaults to False for backwards compatibility - most MCP servers still
388
+ include the structured content in the `tool_result.content`, and using it by
389
+ default will cause duplicate content. You can set this to True if you know the
390
+ server will not duplicate the structured content in the `tool_result.content`.
367
391
"""
368
392
super ().__init__ (
369
393
cache_tools_list ,
370
394
client_session_timeout_seconds ,
371
395
tool_filter ,
396
+ use_structured_content ,
372
397
)
373
398
374
399
self .params = StdioServerParameters (
@@ -429,6 +454,7 @@ def __init__(
429
454
name : str | None = None ,
430
455
client_session_timeout_seconds : float | None = 5 ,
431
456
tool_filter : ToolFilter = None ,
457
+ use_structured_content : bool = False ,
432
458
):
433
459
"""Create a new MCP server based on the HTTP with SSE transport.
434
460
@@ -449,11 +475,17 @@ def __init__(
449
475
450
476
client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
451
477
tool_filter: The tool filter to use for filtering tools.
478
+ use_structured_content: Whether to use `tool_result.structured_content` when calling an
479
+ MCP tool. Defaults to False for backwards compatibility - most MCP servers still
480
+ include the structured content in the `tool_result.content`, and using it by
481
+ default will cause duplicate content. You can set this to True if you know the
482
+ server will not duplicate the structured content in the `tool_result.content`.
452
483
"""
453
484
super ().__init__ (
454
485
cache_tools_list ,
455
486
client_session_timeout_seconds ,
456
487
tool_filter ,
488
+ use_structured_content ,
457
489
)
458
490
459
491
self .params = params
@@ -514,6 +546,7 @@ def __init__(
514
546
name : str | None = None ,
515
547
client_session_timeout_seconds : float | None = 5 ,
516
548
tool_filter : ToolFilter = None ,
549
+ use_structured_content : bool = False ,
517
550
):
518
551
"""Create a new MCP server based on the Streamable HTTP transport.
519
552
@@ -535,11 +568,17 @@ def __init__(
535
568
536
569
client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
537
570
tool_filter: The tool filter to use for filtering tools.
571
+ use_structured_content: Whether to use `tool_result.structured_content` when calling an
572
+ MCP tool. Defaults to False for backwards compatibility - most MCP servers still
573
+ include the structured content in the `tool_result.content`, and using it by
574
+ default will cause duplicate content. You can set this to True if you know the
575
+ server will not duplicate the structured content in the `tool_result.content`.
538
576
"""
539
577
super ().__init__ (
540
578
cache_tools_list ,
541
579
client_session_timeout_seconds ,
542
580
tool_filter ,
581
+ use_structured_content ,
543
582
)
544
583
545
584
self .params = params
0 commit comments