@@ -35,28 +35,35 @@ export const DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME =
3535export interface MCPServer {
3636 cacheToolsList : boolean ;
3737 toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
38+
39+ /**
40+ * Whether to include structuredContent in tool outputs when available.
41+ */
42+ useStructuredContent ?: boolean ;
3843 connect ( ) : Promise < void > ;
3944 readonly name : string ;
4045 close ( ) : Promise < void > ;
4146 listTools ( ) : Promise < MCPTool [ ] > ;
4247 callTool (
4348 toolName : string ,
4449 args : Record < string , unknown > | null ,
45- ) : Promise < CallToolResultContent > ;
50+ ) : Promise < CallToolResult > ;
4651 invalidateToolsCache ( ) : Promise < void > ;
4752}
4853
4954export abstract class BaseMCPServerStdio implements MCPServer {
5055 public cacheToolsList : boolean ;
5156 protected _cachedTools : any [ ] | undefined = undefined ;
5257 public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
58+ public useStructuredContent ?: boolean ;
5359
5460 protected logger : Logger ;
5561 constructor ( options : MCPServerStdioOptions ) {
5662 this . logger =
5763 options . logger ?? getLogger ( DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME ) ;
5864 this . cacheToolsList = options . cacheToolsList ?? false ;
5965 this . toolFilter = options . toolFilter ;
66+ this . useStructuredContent = options . useStructuredContent ?? false ;
6067 }
6168
6269 abstract get name ( ) : string ;
@@ -66,7 +73,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
6673 abstract callTool (
6774 _toolName : string ,
6875 _args : Record < string , unknown > | null ,
69- ) : Promise < CallToolResultContent > ;
76+ ) : Promise < CallToolResult > ;
7077 abstract invalidateToolsCache ( ) : Promise < void > ;
7178
7279 /**
@@ -85,6 +92,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
8592 public cacheToolsList : boolean ;
8693 protected _cachedTools : any [ ] | undefined = undefined ;
8794 public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
95+ public useStructuredContent ?: boolean ;
8896
8997 protected logger : Logger ;
9098 constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -93,6 +101,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
93101 getLogger ( DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME ) ;
94102 this . cacheToolsList = options . cacheToolsList ?? false ;
95103 this . toolFilter = options . toolFilter ;
104+ this . useStructuredContent = options . useStructuredContent ?? false ;
96105 }
97106
98107 abstract get name ( ) : string ;
@@ -102,7 +111,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
102111 abstract callTool (
103112 _toolName : string ,
104113 _args : Record < string , unknown > | null ,
105- ) : Promise < CallToolResultContent > ;
114+ ) : Promise < CallToolResult > ;
106115 abstract invalidateToolsCache ( ) : Promise < void > ;
107116
108117 /**
@@ -121,13 +130,15 @@ export abstract class BaseMCPServerSSE implements MCPServer {
121130 public cacheToolsList : boolean ;
122131 protected _cachedTools : any [ ] | undefined = undefined ;
123132 public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
133+ public useStructuredContent ?: boolean ;
124134
125135 protected logger : Logger ;
126136 constructor ( options : MCPServerSSEOptions ) {
127137 this . logger =
128138 options . logger ?? getLogger ( DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME ) ;
129139 this . cacheToolsList = options . cacheToolsList ?? false ;
130140 this . toolFilter = options . toolFilter ;
141+ this . useStructuredContent = options . useStructuredContent ?? false ;
131142 }
132143
133144 abstract get name ( ) : string ;
@@ -137,7 +148,7 @@ export abstract class BaseMCPServerSSE implements MCPServer {
137148 abstract callTool (
138149 _toolName : string ,
139150 _args : Record < string , unknown > | null ,
140- ) : Promise < CallToolResultContent > ;
151+ ) : Promise < CallToolResult > ;
141152 abstract invalidateToolsCache ( ) : Promise < void > ;
142153
143154 /**
@@ -201,7 +212,7 @@ export class MCPServerStdio extends BaseMCPServerStdio {
201212 callTool (
202213 toolName : string ,
203214 args : Record < string , unknown > | null ,
204- ) : Promise < CallToolResultContent > {
215+ ) : Promise < CallToolResult > {
205216 return this . underlying . callTool ( toolName , args ) ;
206217 }
207218 invalidateToolsCache ( ) : Promise < void > {
@@ -237,7 +248,7 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
237248 callTool (
238249 toolName : string ,
239250 args : Record < string , unknown > | null ,
240- ) : Promise < CallToolResultContent > {
251+ ) : Promise < CallToolResult > {
241252 return this . underlying . callTool ( toolName , args ) ;
242253 }
243254 invalidateToolsCache ( ) : Promise < void > {
@@ -273,7 +284,7 @@ export class MCPServerSSE extends BaseMCPServerSSE {
273284 callTool (
274285 toolName : string ,
275286 args : Record < string , unknown > | null ,
276- ) : Promise < CallToolResultContent > {
287+ ) : Promise < CallToolResult > {
277288 return this . underlying . callTool ( toolName , args ) ;
278289 }
279290 invalidateToolsCache ( ) : Promise < void > {
@@ -446,6 +457,7 @@ export async function getAllMcpTools<TContext = UnknownContext>(
446457
447458/**
448459 * Converts an MCP tool definition to a function tool for the Agents SDK.
460+ * When useStructuredContent is enabled, returns JSON strings for consistency with Python SDK.
449461 */
450462export function mcpToFunctionTool (
451463 mcpTool : MCPTool ,
@@ -463,8 +475,36 @@ export function mcpToFunctionTool(
463475 if ( currentSpan ) {
464476 currentSpan . spanData [ 'mcp_data' ] = { server : server . name } ;
465477 }
466- const content = await server . callTool ( mcpTool . name , args ) ;
467- return content . length === 1 ? content [ 0 ] : content ;
478+ const result = await server . callTool ( mcpTool . name , args ) ;
479+
480+ if ( result . content && result . content . length === 1 ) {
481+ if (
482+ server . useStructuredContent &&
483+ 'structuredContent' in result &&
484+ result . structuredContent !== undefined
485+ ) {
486+ return JSON . stringify ( [ result . content [ 0 ] , result . structuredContent ] ) ;
487+ }
488+ return result . content [ 0 ] ;
489+ } else if ( result . content && result . content . length > 1 ) {
490+ if (
491+ server . useStructuredContent &&
492+ 'structuredContent' in result &&
493+ result . structuredContent !== undefined
494+ ) {
495+ const outputs = [ ...result . content , result . structuredContent ] ;
496+ return JSON . stringify ( outputs ) ;
497+ }
498+ return result . content ;
499+ } else if (
500+ server . useStructuredContent &&
501+ 'structuredContent' in result &&
502+ result . structuredContent !== undefined
503+ ) {
504+ return JSON . stringify ( result . structuredContent ) ;
505+ }
506+ // Preserve backward compatibility: return empty array when no content
507+ return result . content || [ ] ;
468508 }
469509
470510 const schema : JsonObjectSchema < any > = {
@@ -533,6 +573,11 @@ export interface BaseMCPServerStdioOptions {
533573 encodingErrorHandler ?: 'strict' | 'ignore' | 'replace' ;
534574 logger ?: Logger ;
535575 toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
576+
577+ /**
578+ * Whether to include structuredContent in tool outputs when available.
579+ */
580+ useStructuredContent ?: boolean ;
536581 timeout ?: number ;
537582}
538583export interface DefaultMCPServerStdioOptions
@@ -555,6 +600,11 @@ export interface MCPServerStreamableHttpOptions {
555600 name ?: string ;
556601 logger ?: Logger ;
557602 toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
603+
604+ /**
605+ * Whether to include structuredContent in tool outputs when available.
606+ */
607+ useStructuredContent ?: boolean ;
558608 timeout ?: number ;
559609
560610 // ----------------------------------------------------
@@ -579,6 +629,11 @@ export interface MCPServerSSEOptions {
579629 name ?: string ;
580630 logger ?: Logger ;
581631 toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
632+
633+ /**
634+ * Whether to include structuredContent in tool outputs when available.
635+ */
636+ useStructuredContent ?: boolean ;
582637 timeout ?: number ;
583638
584639 // ----------------------------------------------------
@@ -621,9 +676,22 @@ export interface JsonRpcResponse {
621676 error ?: any ;
622677}
623678
679+ /**
680+ * Structured content that can be returned by MCP tools.
681+ * Supports various data types including objects, arrays, primitives, and null.
682+ */
683+ export type StructuredContent =
684+ | Record < string , unknown >
685+ | unknown [ ]
686+ | string
687+ | number
688+ | boolean
689+ | null ;
690+
624691export interface CallToolResponse extends JsonRpcResponse {
625692 result : {
626693 content : { type : string ; text : string } [ ] ;
694+ structuredContent ?: StructuredContent ;
627695 } ;
628696}
629697export type CallToolResult = CallToolResponse [ 'result' ] ;
0 commit comments