fix: call request

This commit is contained in:
gzuuus
2025-03-25 01:04:16 +01:00
parent b6e799e3d9
commit e5c96c7a9b
3 changed files with 40 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ import { type Event } from 'nostr-tools';
import { RelayHandler } from '@dvmcp/commons/nostr/relay-handler';
import { createKeyManager } from '@dvmcp/commons/nostr/key-manager';
import { type Tool } from '@modelcontextprotocol/sdk/types.js';
import { ToolRegistry } from './tool-registry.js';
import {
TOOL_REQUEST_KIND,
TOOL_RESPONSE_KIND,
@@ -19,12 +20,17 @@ export class ToolExecutor {
constructor(
private relayHandler: RelayHandler,
private keyManager: ReturnType<typeof createKeyManager>
private keyManager: ReturnType<typeof createKeyManager>,
private toolRegistry: ToolRegistry
) {}
public async executeTool(tool: Tool, params: unknown): Promise<unknown> {
public async executeTool(
toolId: string,
tool: Tool,
params: unknown
): Promise<unknown> {
return new Promise((resolve, reject) => {
const request = this.createToolRequest(tool, params);
const request = this.createToolRequest(toolId, tool, params);
const executionId = request.id;
const context = this.createExecutionContext(executionId);
@@ -102,7 +108,11 @@ export class ToolExecutor {
}
}
private createToolRequest(tool: Tool, params: unknown): Event {
private createToolRequest(
toolId: string,
tool: Tool,
params: unknown
): Event {
const request = this.keyManager.createEventTemplate(TOOL_REQUEST_KIND);
const parameters =
@@ -111,12 +121,17 @@ export class ToolExecutor {
? {}
: params;
const toolInfo = this.toolRegistry.getToolInfo(toolId);
if (!toolInfo) throw new Error(`Tool ${toolId} not found`);
request.content = JSON.stringify({
name: tool.name,
parameters,
providerPubkey: toolInfo.providerPubkey,
});
request.tags.push(['c', 'execute-tool']);
request.tags.push(['p', toolInfo.providerPubkey]);
return this.keyManager.signEvent(request);
}
}