2828class MCPServer (abc .ABC ):
2929 """Base class for Model Context Protocol servers."""
3030
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+
3142 @abc .abstractmethod
3243 async def connect (self ):
3344 """Connect to the server. For example, this might mean spawning a subprocess or
@@ -86,6 +97,7 @@ def __init__(
8697 cache_tools_list : bool ,
8798 client_session_timeout_seconds : float | None ,
8899 tool_filter : ToolFilter = None ,
100+ use_structured_content : bool = False ,
89101 ):
90102 """
91103 Args:
@@ -98,7 +110,13 @@ def __init__(
98110
99111 client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
100112 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`.
101118 """
119+ super ().__init__ (use_structured_content = use_structured_content )
102120 self .session : ClientSession | None = None
103121 self .exit_stack : AsyncExitStack = AsyncExitStack ()
104122 self ._cleanup_lock : asyncio .Lock = asyncio .Lock ()
@@ -346,6 +364,7 @@ def __init__(
346364 name : str | None = None ,
347365 client_session_timeout_seconds : float | None = 5 ,
348366 tool_filter : ToolFilter = None ,
367+ use_structured_content : bool = False ,
349368 ):
350369 """Create a new MCP server based on the stdio transport.
351370
@@ -364,11 +383,17 @@ def __init__(
364383 command.
365384 client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
366385 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`.
367391 """
368392 super ().__init__ (
369393 cache_tools_list ,
370394 client_session_timeout_seconds ,
371395 tool_filter ,
396+ use_structured_content ,
372397 )
373398
374399 self .params = StdioServerParameters (
@@ -429,6 +454,7 @@ def __init__(
429454 name : str | None = None ,
430455 client_session_timeout_seconds : float | None = 5 ,
431456 tool_filter : ToolFilter = None ,
457+ use_structured_content : bool = False ,
432458 ):
433459 """Create a new MCP server based on the HTTP with SSE transport.
434460
@@ -449,11 +475,17 @@ def __init__(
449475
450476 client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
451477 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`.
452483 """
453484 super ().__init__ (
454485 cache_tools_list ,
455486 client_session_timeout_seconds ,
456487 tool_filter ,
488+ use_structured_content ,
457489 )
458490
459491 self .params = params
@@ -514,6 +546,7 @@ def __init__(
514546 name : str | None = None ,
515547 client_session_timeout_seconds : float | None = 5 ,
516548 tool_filter : ToolFilter = None ,
549+ use_structured_content : bool = False ,
517550 ):
518551 """Create a new MCP server based on the Streamable HTTP transport.
519552
@@ -535,11 +568,17 @@ def __init__(
535568
536569 client_session_timeout_seconds: the read timeout passed to the MCP ClientSession.
537570 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`.
538576 """
539577 super ().__init__ (
540578 cache_tools_list ,
541579 client_session_timeout_seconds ,
542580 tool_filter ,
581+ use_structured_content ,
543582 )
544583
545584 self .params = params
0 commit comments