mirror of
https://github.com/hanzoai/code.git
synced 2026-09-30 20:53:35 +00:00
Added functions that return properly formatted CallTool functions and ToolResultToStr functions from MCP tools, then hooked it up
This commit is contained in:
@@ -19,6 +19,7 @@ import { RawToolParamsObj } from '../common/sendLLMMessageTypes.js'
|
||||
import { MAX_CHILDREN_URIs_PAGE, MAX_FILE_CHARS_PAGE, MAX_TERMINAL_BG_COMMAND_TIME, MAX_TERMINAL_INACTIVE_TIME, ToolName } from '../common/prompt/prompts.js'
|
||||
import { IVoidSettingsService } from '../common/voidSettingsService.js'
|
||||
import { generateUuid } from '../../../../base/common/uuid.js'
|
||||
import { IMCPService, MCPCallTool, MCPToolResultToString } from '../common/mcpService.js'
|
||||
|
||||
|
||||
// tool use for AI
|
||||
@@ -30,6 +31,10 @@ type ValidateParams = { [T in ToolName]: (p: RawToolParamsObj) => ToolCallParams
|
||||
type CallTool = { [T in ToolName]: (p: ToolCallParams[T]) => Promise<{ result: ToolResultType[T] | Promise<ToolResultType[T]>, interruptTool?: () => void }> }
|
||||
type ToolResultToString = { [T in ToolName]: (p: ToolCallParams[T], result: Awaited<ToolResultType[T]>) => string }
|
||||
|
||||
// Interfaces that accept both internal tools and MCP tools
|
||||
export type ToolHandler = CallTool & MCPCallTool;
|
||||
export type ToolResultToStringHandler = ToolResultToString & MCPToolResultToString
|
||||
|
||||
|
||||
|
||||
const isFalsy = (u: unknown) => {
|
||||
@@ -111,8 +116,8 @@ const checkIfIsFolder = (uriStr: string) => {
|
||||
export interface IToolsService {
|
||||
readonly _serviceBrand: undefined;
|
||||
validateParams: ValidateParams;
|
||||
callTool: CallTool;
|
||||
stringOfResult: ToolResultToString;
|
||||
callTool: ToolHandler;
|
||||
stringOfResult: ToolResultToStringHandler;
|
||||
}
|
||||
|
||||
export const IToolsService = createDecorator<IToolsService>('ToolsService');
|
||||
@@ -122,8 +127,8 @@ export class ToolsService implements IToolsService {
|
||||
readonly _serviceBrand: undefined;
|
||||
|
||||
public validateParams: ValidateParams;
|
||||
public callTool: CallTool;
|
||||
public stringOfResult: ToolResultToString;
|
||||
public callTool: ToolHandler;
|
||||
public stringOfResult: ToolResultToStringHandler;
|
||||
|
||||
constructor(
|
||||
@IFileService fileService: IFileService,
|
||||
@@ -137,6 +142,7 @@ export class ToolsService implements IToolsService {
|
||||
@IDirectoryStrService private readonly directoryStrService: IDirectoryStrService,
|
||||
@IMarkerService private readonly markerService: IMarkerService,
|
||||
@IVoidSettingsService private readonly voidSettingsService: IVoidSettingsService,
|
||||
@IMCPService private readonly mcpService: IMCPService,
|
||||
) {
|
||||
|
||||
const queryBuilder = instantiationService.createInstance(QueryBuilder);
|
||||
@@ -446,7 +452,8 @@ export class ToolsService implements IToolsService {
|
||||
await this.terminalToolService.killPersistentTerminal(persistentTerminalId)
|
||||
return { result: {} }
|
||||
},
|
||||
|
||||
// Returns MCP server call tool functions
|
||||
...this.mcpService.getMCPToolFns().callTool,
|
||||
}
|
||||
|
||||
|
||||
@@ -550,7 +557,8 @@ export class ToolsService implements IToolsService {
|
||||
kill_persistent_terminal: (params, _result) => {
|
||||
return `Successfully closed terminal "${params.persistentTerminalId}".`;
|
||||
},
|
||||
|
||||
// All MCP server call tool functions
|
||||
...this.mcpService.getMCPToolFns().resultToString,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -32,10 +32,25 @@ export interface IMCPService {
|
||||
onDidDeleteServer: Event<MCPServerEventDeleteParam>;
|
||||
onLoadingServers: Event<MCPServerEventLoadingParam>;
|
||||
onConfigParsingError: Event<MCPConfigParseError>;
|
||||
getMCPToolFns(): {
|
||||
callTool: MCPCallTool;
|
||||
resultToString: MCPToolResultToString
|
||||
};
|
||||
}
|
||||
|
||||
export const IMCPService = createDecorator<IMCPService>('mcpConfigService');
|
||||
|
||||
export interface MCPCallTool {
|
||||
[toolName: string]: (params: any) => Promise<{
|
||||
result: any | Promise<any>,
|
||||
interruptTool?: () => void
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface MCPToolResultToString {
|
||||
[toolName: string]: (params: any, result: any) => string;
|
||||
}
|
||||
|
||||
class MCPService extends Disposable implements IMCPService {
|
||||
_serviceBrand: undefined;
|
||||
|
||||
@@ -266,8 +281,9 @@ class MCPService extends Disposable implements IMCPService {
|
||||
}
|
||||
|
||||
public getAllToolsFormatted(): InternalToolInfo[] {
|
||||
const allTools = Object.values(this.mcpServers).flatMap(server => {
|
||||
return server.tools.map(tool => {
|
||||
const allTools = Object.keys(this.mcpServers).flatMap(serverName => {
|
||||
const server = this.mcpServers[serverName];
|
||||
return server.tools.map((tool) => {
|
||||
// Convert JsonSchema to the expected format
|
||||
const convertedParams: { [paramName: string]: { description: string } } = {};
|
||||
|
||||
@@ -284,6 +300,7 @@ class MCPService extends Disposable implements IMCPService {
|
||||
description: tool.description || '',
|
||||
params: convertedParams,
|
||||
name: tool.name,
|
||||
serverName,
|
||||
};
|
||||
});
|
||||
});
|
||||
@@ -353,6 +370,50 @@ class MCPService extends Disposable implements IMCPService {
|
||||
const response = await this.channel.call<MCPGenericToolResponse>('callTool', toolData);
|
||||
return response;
|
||||
}
|
||||
|
||||
public getMCPToolFns(): { callTool: MCPCallTool; resultToString: MCPToolResultToString } {
|
||||
const tools = this.getAllToolsFormatted();
|
||||
const toolFns: MCPCallTool = {};
|
||||
const toolResultToStringFns: MCPToolResultToString = {};
|
||||
|
||||
tools.forEach((tool) => {
|
||||
const name = tool.name;
|
||||
const serverName = tool.mcpServerName;
|
||||
|
||||
// Define the tool call function
|
||||
const toolFn = async (params: {
|
||||
serverName: string,
|
||||
toolName: string,
|
||||
args: any
|
||||
}) => {
|
||||
const { serverName, toolName, args } = params;
|
||||
const response = await this.callMCPTool({
|
||||
serverName,
|
||||
toolName,
|
||||
params: args,
|
||||
});
|
||||
return {
|
||||
result: response,
|
||||
};
|
||||
};
|
||||
|
||||
// Define the result-to-string function
|
||||
const resultToStringFn = (params: any, result: MCPGenericToolResponse): string => {
|
||||
if (result.event === 'error' || result.event === 'text') {
|
||||
return result.text;
|
||||
}
|
||||
throw new Error(`MCP Server ${serverName} and Tool ${name} returned an unexpected result: ${JSON.stringify(result)}`);
|
||||
};
|
||||
|
||||
toolFns[name] = toolFn;
|
||||
toolResultToStringFns[name] = resultToStringFn;
|
||||
});
|
||||
|
||||
return {
|
||||
callTool: toolFns,
|
||||
resultToString: toolResultToStringFns
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
registerSingleton(IMCPService, MCPService, InstantiationType.Eager);
|
||||
|
||||
Reference in New Issue
Block a user