Skip to content

Commit 7ce00b0

Browse files
committed
feat: add sendToolResult function for MCP format handling
- Introduced global.sendToolResult function to send tool results in MCP format. - Supports both string content and structured CallToolResult objects. - Updated type definitions to include sendToolResult with detailed documentation and examples.
1 parent da1ffd6 commit 7ce00b0

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/api/kit.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { getScripts, getScriptFromString, getUserJson, getTimestamps, type Stamp
4040

4141
import { default as stripAnsi } from 'strip-ansi'
4242

43-
import type { Kenv } from '../types/kit'
43+
import type { CallToolResult, Kenv } from '../types/kit'
4444
import type { Fields as TraceFields } from 'chrome-trace-event'
4545
import dotenv from 'dotenv'
4646
import type { kenvEnv } from '../types/env'
@@ -318,6 +318,35 @@ global.sendResponse = (body: any, headers: Record<string, string> = {}) => {
318318
return global.sendWait(Channel.RESPONSE, response)
319319
}
320320

321+
global.sendToolResult = (content: string | CallToolResult) => {
322+
// Format content according to MCP tool result specification
323+
let toolResult: CallToolResult;
324+
325+
if (typeof content === 'string') {
326+
// If content is a string, wrap it in the MCP format
327+
toolResult = {
328+
content: [
329+
{
330+
type: 'text',
331+
text: content
332+
}
333+
]
334+
};
335+
} else {
336+
// Content is already in MCP format
337+
toolResult = content;
338+
}
339+
340+
// Send the tool result using the same RESPONSE channel
341+
return global.sendWait(Channel.RESPONSE, {
342+
body: toolResult,
343+
statusCode: 200,
344+
headers: {
345+
'Content-Type': 'application/json'
346+
}
347+
});
348+
}
349+
321350
// Import and export params function
322351
import { params } from './params.js'
323352
global.params = params

src/types/kitapp.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type Script,
2020
type Shortcut
2121
} from './core.js'
22+
import type { CallToolResult } from './kit'
2223
import type { BrowserWindowConstructorOptions, Display, Rectangle } from './electron.js'
2324

2425
import type { Trash } from './packages.js'
@@ -1296,6 +1297,33 @@ declare global {
12961297
var sendWait: (channel: Channel, value?: any, timeout?: number) => Promise<any>
12971298
var headers: Record<string, string>
12981299
var sendResponse: (body: any, headers?: Record<string, string>) => Promise<any>
1300+
/**
1301+
* Send a tool result in MCP (Model Context Protocol) format
1302+
* @param content - The content to send. Can be a string (automatically formatted) or a CallToolResult object
1303+
* @returns Promise that resolves when the result is sent
1304+
* @example
1305+
* ```ts
1306+
* // Send a simple string result
1307+
* await sendToolResult("Hello from MCP tool!")
1308+
*
1309+
* // Send a structured MCP result
1310+
* await sendToolResult({
1311+
* content: [{
1312+
* type: 'text',
1313+
* text: 'Processed successfully'
1314+
* }]
1315+
* })
1316+
*
1317+
* // Send multiple content items
1318+
* await sendToolResult({
1319+
* content: [
1320+
* { type: 'text', text: 'Analysis complete.' },
1321+
* { type: 'image', data: imageBase64, mimeType: 'image/png' }
1322+
* ]
1323+
* })
1324+
* ```
1325+
*/
1326+
var sendToolResult: (content: string | CallToolResult) => Promise<any>
12991327
var sendWaitLong: (channel: Channel, value?: any, timeout?: number) => Promise<any>
13001328

13011329
var setFocused: SetFocused

0 commit comments

Comments
 (0)