Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/mcp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import type { ActorMcpTool, ActorTool, HelperTool, ToolEntry } from '../types.js
import { buildActorResponseContent } from '../utils/actor-response.js';
import { buildMCPResponse } from '../utils/mcp.js';
import { createProgressTracker } from '../utils/progress.js';
import { getToolPublicFieldOnly } from '../utils/tools.js';
import { cloneToolEntry, getToolPublicFieldOnly } from '../utils/tools.js';
import { connectMCPClient } from './client.js';
import { EXTERNAL_TOOL_CALL_TIMEOUT_MSEC, LOG_LEVEL_MAP } from './const.js';
import { processParamsGetTools } from './utils.js';
Expand Down Expand Up @@ -262,31 +262,42 @@ export class ActorsMcpServer {
* @returns Array of added/updated tool wrappers
*/
public upsertTools(tools: ToolEntry[], shouldNotifyToolsChangedHandler = false) {
for (const wrap of tools) {
this.tools.set(wrap.tool.name, wrap);
}
// Handle Skyfire mode modifications once per tool upsert
// Handle Skyfire mode modifications before storing tools
if (this.options.skyfireMode) {
for (const wrap of tools) {
if (wrap.type === 'actor'
|| (wrap.type === 'internal' && wrap.tool.name === HelperTools.ACTOR_CALL)
|| (wrap.type === 'internal' && wrap.tool.name === HelperTools.ACTOR_OUTPUT_GET)) {
// Clone the tool before modifying it to avoid affecting shared objects
const clonedWrap = cloneToolEntry(wrap);

// Add Skyfire instructions to description if not already present
if (!wrap.tool.description.includes(SKYFIRE_TOOL_INSTRUCTIONS)) {
wrap.tool.description += `\n\n${SKYFIRE_TOOL_INSTRUCTIONS}`;
if (!clonedWrap.tool.description.includes(SKYFIRE_TOOL_INSTRUCTIONS)) {
clonedWrap.tool.description += `\n\n${SKYFIRE_TOOL_INSTRUCTIONS}`;
}
// Add skyfire-pay-id property if not present
if (wrap.tool.inputSchema && 'properties' in wrap.tool.inputSchema) {
const props = wrap.tool.inputSchema.properties as Record<string, unknown>;
if (clonedWrap.tool.inputSchema && 'properties' in clonedWrap.tool.inputSchema) {
const props = clonedWrap.tool.inputSchema.properties as Record<string, unknown>;
if (!props['skyfire-pay-id']) {
props['skyfire-pay-id'] = {
type: 'string',
description: SKYFIRE_PAY_ID_PROPERTY_DESCRIPTION,
};
}
}

// Store the cloned and modified tool
this.tools.set(clonedWrap.tool.name, clonedWrap);
} else {
// Store unmodified tools as-is
this.tools.set(wrap.tool.name, wrap);
}
}
} else {
// No skyfire mode - store tools as-is
for (const wrap of tools) {
this.tools.set(wrap.tool.name, wrap);
}
}
if (shouldNotifyToolsChangedHandler) this.notifyToolsChangedHandler();
return tools;
Expand Down
26 changes: 25 additions & 1 deletion src/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toolCategories } from '../tools/index.js';
import type { ToolBase, ToolCategory, ToolEntry } from '../types.js';
import type { InternalTool, ToolBase, ToolCategory, ToolEntry } from '../types.js';

/**
* Returns a public version of the tool containing only fields that should be exposed publicly.
Expand Down Expand Up @@ -27,3 +27,27 @@ export function getExpectedToolsByCategories(categories: ToolCategory[]): ToolEn
export function getExpectedToolNamesByCategories(categories: ToolCategory[]): string[] {
return getExpectedToolsByCategories(categories).map((tool) => tool.tool.name);
}

/**
* Creates a deep copy of a tool entry, preserving functions like ajvValidate and call
* while cloning all other properties to avoid shared state mutations.
*/
export function cloneToolEntry(toolEntry: ToolEntry): ToolEntry {
// Store the original functions
const originalAjvValidate = toolEntry.tool.ajvValidate;
const originalCall = toolEntry.type === 'internal' ? (toolEntry.tool as InternalTool).call : undefined;

// Create a deep copy using JSON serialization (excluding functions)
const cloned = JSON.parse(JSON.stringify(toolEntry, (key, value) => {
if (key === 'ajvValidate' || key === 'call') return undefined;
return value;
})) as ToolEntry;

// Restore the original functions
cloned.tool.ajvValidate = originalAjvValidate;
if (toolEntry.type === 'internal' && originalCall) {
(cloned.tool as InternalTool).call = originalCall;
}

return cloned;
}
Loading