@@ -35,28 +35,35 @@ export const DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME =
35
35
export interface MCPServer {
36
36
cacheToolsList : boolean ;
37
37
toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
38
+
39
+ /**
40
+ * Whether to include structuredContent in tool outputs when available.
41
+ */
42
+ useStructuredContent ?: boolean ;
38
43
connect ( ) : Promise < void > ;
39
44
readonly name : string ;
40
45
close ( ) : Promise < void > ;
41
46
listTools ( ) : Promise < MCPTool [ ] > ;
42
47
callTool (
43
48
toolName : string ,
44
49
args : Record < string , unknown > | null ,
45
- ) : Promise < CallToolResultContent > ;
50
+ ) : Promise < CallToolResult > ;
46
51
invalidateToolsCache ( ) : Promise < void > ;
47
52
}
48
53
49
54
export abstract class BaseMCPServerStdio implements MCPServer {
50
55
public cacheToolsList : boolean ;
51
56
protected _cachedTools : any [ ] | undefined = undefined ;
52
57
public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
58
+ public useStructuredContent ?: boolean ;
53
59
54
60
protected logger : Logger ;
55
61
constructor ( options : MCPServerStdioOptions ) {
56
62
this . logger =
57
63
options . logger ?? getLogger ( DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME ) ;
58
64
this . cacheToolsList = options . cacheToolsList ?? false ;
59
65
this . toolFilter = options . toolFilter ;
66
+ this . useStructuredContent = options . useStructuredContent ?? false ;
60
67
}
61
68
62
69
abstract get name ( ) : string ;
@@ -66,7 +73,7 @@ export abstract class BaseMCPServerStdio implements MCPServer {
66
73
abstract callTool (
67
74
_toolName : string ,
68
75
_args : Record < string , unknown > | null ,
69
- ) : Promise < CallToolResultContent > ;
76
+ ) : Promise < CallToolResult > ;
70
77
abstract invalidateToolsCache ( ) : Promise < void > ;
71
78
72
79
/**
@@ -85,6 +92,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
85
92
public cacheToolsList : boolean ;
86
93
protected _cachedTools : any [ ] | undefined = undefined ;
87
94
public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
95
+ public useStructuredContent ?: boolean ;
88
96
89
97
protected logger : Logger ;
90
98
constructor ( options : MCPServerStreamableHttpOptions ) {
@@ -93,6 +101,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
93
101
getLogger ( DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME ) ;
94
102
this . cacheToolsList = options . cacheToolsList ?? false ;
95
103
this . toolFilter = options . toolFilter ;
104
+ this . useStructuredContent = options . useStructuredContent ?? false ;
96
105
}
97
106
98
107
abstract get name ( ) : string ;
@@ -102,7 +111,7 @@ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
102
111
abstract callTool (
103
112
_toolName : string ,
104
113
_args : Record < string , unknown > | null ,
105
- ) : Promise < CallToolResultContent > ;
114
+ ) : Promise < CallToolResult > ;
106
115
abstract invalidateToolsCache ( ) : Promise < void > ;
107
116
108
117
/**
@@ -121,13 +130,15 @@ export abstract class BaseMCPServerSSE implements MCPServer {
121
130
public cacheToolsList : boolean ;
122
131
protected _cachedTools : any [ ] | undefined = undefined ;
123
132
public toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
133
+ public useStructuredContent ?: boolean ;
124
134
125
135
protected logger : Logger ;
126
136
constructor ( options : MCPServerSSEOptions ) {
127
137
this . logger =
128
138
options . logger ?? getLogger ( DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME ) ;
129
139
this . cacheToolsList = options . cacheToolsList ?? false ;
130
140
this . toolFilter = options . toolFilter ;
141
+ this . useStructuredContent = options . useStructuredContent ?? false ;
131
142
}
132
143
133
144
abstract get name ( ) : string ;
@@ -137,7 +148,7 @@ export abstract class BaseMCPServerSSE implements MCPServer {
137
148
abstract callTool (
138
149
_toolName : string ,
139
150
_args : Record < string , unknown > | null ,
140
- ) : Promise < CallToolResultContent > ;
151
+ ) : Promise < CallToolResult > ;
141
152
abstract invalidateToolsCache ( ) : Promise < void > ;
142
153
143
154
/**
@@ -201,7 +212,7 @@ export class MCPServerStdio extends BaseMCPServerStdio {
201
212
callTool (
202
213
toolName : string ,
203
214
args : Record < string , unknown > | null ,
204
- ) : Promise < CallToolResultContent > {
215
+ ) : Promise < CallToolResult > {
205
216
return this . underlying . callTool ( toolName , args ) ;
206
217
}
207
218
invalidateToolsCache ( ) : Promise < void > {
@@ -237,7 +248,7 @@ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
237
248
callTool (
238
249
toolName : string ,
239
250
args : Record < string , unknown > | null ,
240
- ) : Promise < CallToolResultContent > {
251
+ ) : Promise < CallToolResult > {
241
252
return this . underlying . callTool ( toolName , args ) ;
242
253
}
243
254
invalidateToolsCache ( ) : Promise < void > {
@@ -273,7 +284,7 @@ export class MCPServerSSE extends BaseMCPServerSSE {
273
284
callTool (
274
285
toolName : string ,
275
286
args : Record < string , unknown > | null ,
276
- ) : Promise < CallToolResultContent > {
287
+ ) : Promise < CallToolResult > {
277
288
return this . underlying . callTool ( toolName , args ) ;
278
289
}
279
290
invalidateToolsCache ( ) : Promise < void > {
@@ -446,6 +457,7 @@ export async function getAllMcpTools<TContext = UnknownContext>(
446
457
447
458
/**
448
459
* 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.
449
461
*/
450
462
export function mcpToFunctionTool (
451
463
mcpTool : MCPTool ,
@@ -463,8 +475,36 @@ export function mcpToFunctionTool(
463
475
if ( currentSpan ) {
464
476
currentSpan . spanData [ 'mcp_data' ] = { server : server . name } ;
465
477
}
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 || [ ] ;
468
508
}
469
509
470
510
const schema : JsonObjectSchema < any > = {
@@ -533,6 +573,11 @@ export interface BaseMCPServerStdioOptions {
533
573
encodingErrorHandler ?: 'strict' | 'ignore' | 'replace' ;
534
574
logger ?: Logger ;
535
575
toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
576
+
577
+ /**
578
+ * Whether to include structuredContent in tool outputs when available.
579
+ */
580
+ useStructuredContent ?: boolean ;
536
581
timeout ?: number ;
537
582
}
538
583
export interface DefaultMCPServerStdioOptions
@@ -555,6 +600,11 @@ export interface MCPServerStreamableHttpOptions {
555
600
name ?: string ;
556
601
logger ?: Logger ;
557
602
toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
603
+
604
+ /**
605
+ * Whether to include structuredContent in tool outputs when available.
606
+ */
607
+ useStructuredContent ?: boolean ;
558
608
timeout ?: number ;
559
609
560
610
// ----------------------------------------------------
@@ -579,6 +629,11 @@ export interface MCPServerSSEOptions {
579
629
name ?: string ;
580
630
logger ?: Logger ;
581
631
toolFilter ?: MCPToolFilterCallable | MCPToolFilterStatic ;
632
+
633
+ /**
634
+ * Whether to include structuredContent in tool outputs when available.
635
+ */
636
+ useStructuredContent ?: boolean ;
582
637
timeout ?: number ;
583
638
584
639
// ----------------------------------------------------
@@ -621,9 +676,22 @@ export interface JsonRpcResponse {
621
676
error ?: any ;
622
677
}
623
678
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
+
624
691
export interface CallToolResponse extends JsonRpcResponse {
625
692
result : {
626
693
content : { type : string ; text : string } [ ] ;
694
+ structuredContent ?: StructuredContent ;
627
695
} ;
628
696
}
629
697
export type CallToolResult = CallToolResponse [ 'result' ] ;
0 commit comments