mirror of
https://github.com/aljazceru/dvmcp.git
synced 2025-12-17 13:24:24 +01:00
fix: call request
This commit is contained in:
@@ -31,12 +31,16 @@ export class DiscoveryServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.toolRegistry = new ToolRegistry(this.mcpServer);
|
this.toolRegistry = new ToolRegistry(this.mcpServer);
|
||||||
this.toolExecutor = new ToolExecutor(this.relayHandler, this.keyManager);
|
this.toolExecutor = new ToolExecutor(
|
||||||
|
this.relayHandler,
|
||||||
|
this.keyManager,
|
||||||
|
this.toolRegistry
|
||||||
|
);
|
||||||
|
|
||||||
this.toolRegistry.setExecutionCallback(async (toolId, args) => {
|
this.toolRegistry.setExecutionCallback(async (toolId, args) => {
|
||||||
const tool = this.toolRegistry.getTool(toolId);
|
const tool = this.toolRegistry.getTool(toolId);
|
||||||
if (!tool) throw new Error('Tool not found');
|
if (!tool) throw new Error('Tool not found');
|
||||||
return this.toolExecutor.executeTool(tool, args);
|
return this.toolExecutor.executeTool(toolId, tool, args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +66,7 @@ export class DiscoveryServer {
|
|||||||
|
|
||||||
for (const tool of announcement.tools) {
|
for (const tool of announcement.tools) {
|
||||||
const toolId = `${tool.name}:${event.pubkey.slice(0, 4)}`;
|
const toolId = `${tool.name}:${event.pubkey.slice(0, 4)}`;
|
||||||
this.toolRegistry.registerTool(toolId, tool);
|
this.toolRegistry.registerTool(toolId, tool, event.pubkey);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing DVM announcement:', error);
|
console.error('Error processing DVM announcement:', error);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { type Event } from 'nostr-tools';
|
|||||||
import { RelayHandler } from '@dvmcp/commons/nostr/relay-handler';
|
import { RelayHandler } from '@dvmcp/commons/nostr/relay-handler';
|
||||||
import { createKeyManager } from '@dvmcp/commons/nostr/key-manager';
|
import { createKeyManager } from '@dvmcp/commons/nostr/key-manager';
|
||||||
import { type Tool } from '@modelcontextprotocol/sdk/types.js';
|
import { type Tool } from '@modelcontextprotocol/sdk/types.js';
|
||||||
|
import { ToolRegistry } from './tool-registry.js';
|
||||||
import {
|
import {
|
||||||
TOOL_REQUEST_KIND,
|
TOOL_REQUEST_KIND,
|
||||||
TOOL_RESPONSE_KIND,
|
TOOL_RESPONSE_KIND,
|
||||||
@@ -19,12 +20,17 @@ export class ToolExecutor {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private relayHandler: RelayHandler,
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
const request = this.createToolRequest(tool, params);
|
const request = this.createToolRequest(toolId, tool, params);
|
||||||
const executionId = request.id;
|
const executionId = request.id;
|
||||||
const context = this.createExecutionContext(executionId);
|
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 request = this.keyManager.createEventTemplate(TOOL_REQUEST_KIND);
|
||||||
|
|
||||||
const parameters =
|
const parameters =
|
||||||
@@ -111,12 +121,17 @@ export class ToolExecutor {
|
|||||||
? {}
|
? {}
|
||||||
: params;
|
: params;
|
||||||
|
|
||||||
|
const toolInfo = this.toolRegistry.getToolInfo(toolId);
|
||||||
|
if (!toolInfo) throw new Error(`Tool ${toolId} not found`);
|
||||||
|
|
||||||
request.content = JSON.stringify({
|
request.content = JSON.stringify({
|
||||||
name: tool.name,
|
name: tool.name,
|
||||||
parameters,
|
parameters,
|
||||||
|
providerPubkey: toolInfo.providerPubkey,
|
||||||
});
|
});
|
||||||
|
|
||||||
request.tags.push(['c', 'execute-tool']);
|
request.tags.push(['c', 'execute-tool']);
|
||||||
|
request.tags.push(['p', toolInfo.providerPubkey]);
|
||||||
return this.keyManager.signEvent(request);
|
return this.keyManager.signEvent(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,19 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export class ToolRegistry {
|
export class ToolRegistry {
|
||||||
private discoveredTools: Map<string, Tool> = new Map();
|
private discoveredTools: Map<string, { tool: Tool; providerPubkey: string }> =
|
||||||
|
new Map();
|
||||||
|
|
||||||
constructor(private mcpServer: McpServer) {}
|
constructor(private mcpServer: McpServer) {}
|
||||||
|
|
||||||
public registerTool(toolId: string, tool: Tool): void {
|
public registerTool(
|
||||||
|
toolId: string,
|
||||||
|
tool: Tool,
|
||||||
|
providerPubkey: string
|
||||||
|
): void {
|
||||||
try {
|
try {
|
||||||
ToolSchema.parse(tool);
|
ToolSchema.parse(tool);
|
||||||
this.discoveredTools.set(toolId, tool);
|
this.discoveredTools.set(toolId, { tool, providerPubkey });
|
||||||
this.registerWithMcp(toolId, tool);
|
this.registerWithMcp(toolId, tool);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Invalid MCP tool format for ${toolId}:`, error);
|
console.error(`Invalid MCP tool format for ${toolId}:`, error);
|
||||||
@@ -19,12 +24,16 @@ export class ToolRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTool(toolId: string): Tool | undefined {
|
public getToolInfo(toolId: string) {
|
||||||
return this.discoveredTools.get(toolId);
|
return this.discoveredTools.get(toolId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getTool(toolId: string): Tool | undefined {
|
||||||
|
return this.discoveredTools.get(toolId)?.tool;
|
||||||
|
}
|
||||||
|
|
||||||
public listTools(): Tool[] {
|
public listTools(): Tool[] {
|
||||||
return Array.from(this.discoveredTools.values());
|
return Array.from(this.discoveredTools.values()).map(({ tool }) => tool);
|
||||||
}
|
}
|
||||||
|
|
||||||
public clear(): void {
|
public clear(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user