1
1
import { Client } from "@modelcontextprotocol/sdk/client/index.js" ;
2
+ import {
3
+ CallToolResult ,
4
+ TextContent ,
5
+ } from "@modelcontextprotocol/sdk/types.js" ;
2
6
import { Tool } from "./tool.js" ;
3
7
import logger from "../logger.js" ;
4
- import { ToolResultBlock } from "@aws-sdk/client-bedrock-runtime" ;
8
+ import {
9
+ ToolResultBlock ,
10
+ ToolResultContentBlock ,
11
+ } from "@aws-sdk/client-bedrock-runtime" ;
5
12
6
13
/**
7
14
* Abstract base class for communicating with an MCP server.
@@ -79,27 +86,36 @@ export abstract class Server {
79
86
while ( attempt < retries ) {
80
87
try {
81
88
logger . info ( `Executing ${ toolName } ...` ) ;
82
- const result = await this . client . callTool ( {
89
+ const result : CallToolResult = ( await this . client . callTool ( {
83
90
name : toolName ,
84
91
arguments : args ,
85
- } ) ;
92
+ } ) ) as CallToolResult ;
86
93
logger . info ( `Finished executing ${ toolName } ` ) ;
87
94
88
- if ( result && typeof result === "object" && "progress" in result ) {
89
- const progress = result . progress as number ;
90
- const total = result . total as number ;
91
- const percentage = ( progress / total ) * 100 ;
92
- logger . info (
93
- `Progress: ${ progress } /${ total } (${ percentage . toFixed ( 1 ) } %)`
94
- ) ;
95
- throw new Error (
96
- "Does not support progress notifications from tools yet"
97
- ) ;
95
+ if ( result . isError ) {
96
+ throw new Error ( `Error executing tool: ${ JSON . stringify ( result ) } ` ) ;
97
+ }
98
+
99
+ if ( result . structuredContent ) {
100
+ return {
101
+ toolUseId : toolUseId ,
102
+ content : [ { text : JSON . stringify ( result . structuredContent ) } ] ,
103
+ status : "success" ,
104
+ } ;
105
+ }
106
+
107
+ const content : ToolResultContentBlock [ ] = [ ] ;
108
+ for ( const block of result . content ) {
109
+ if ( block . type === "text" ) {
110
+ content . push ( { text : ( block as TextContent ) . text } ) ;
111
+ } else {
112
+ throw new Error ( `Unexpected content type: ${ block . type } ` ) ;
113
+ }
98
114
}
99
115
100
116
return {
101
117
toolUseId : toolUseId ,
102
- content : [ { text : this . formatText ( result ) } ] ,
118
+ content,
103
119
status : "success" ,
104
120
} ;
105
121
} catch ( e ) {
@@ -124,16 +140,4 @@ export abstract class Server {
124
140
// This should never be reached due to the loop above
125
141
throw new Error ( "Unexpected error in executeTool" ) ;
126
142
}
127
-
128
- formatText ( text : unknown ) : string {
129
- if ( typeof text === "string" ) {
130
- return text ;
131
- } else if ( Array . isArray ( text ) ) {
132
- return text . map ( ( item ) => this . formatText ( item ) ) . join ( "\n" ) ;
133
- } else if ( typeof text === "object" && text !== null ) {
134
- return JSON . stringify ( text ) ;
135
- } else {
136
- return String ( text ) ;
137
- }
138
- }
139
143
}
0 commit comments