@stanley2058/local-reflect-mcp
v1.0.0
Published
This is a simple tool to reflect server side tool calls for multilayer tool calling.
Readme
local-reflect-mcp
This is a simple tool to reflect server side tool calls for multilayer tool calling.
If you are like me and have the following requirement:
- You have a server with tools.
- You have a client also with tools.
- You want to be able to call both client and server tools without shadowing any of them.
On your server you can replace reflect tool calls with the actual tool name to make the model think it's calling the correct tool. Example code using AI-SDK:
type ReflectIOShape = {
id: string;
name: string;
input: unknown;
output: unknown;
};
function convertServerSideToolCallsWithReflection(
messages: ModelMessage[],
reflectionToolName: string,
) {
const toolResultMap = new Map<string, ReflectIOShape>();
const output = structuredClone(messages);
for (let i = 0; i < output.length; i++) {
const message = output[i]!;
if (message.role === "assistant") {
if (typeof message.content === "string") continue;
for (let j = 0; j < message.content.length; j++) {
const content = message.content[j]!;
if (content.type === "tool-call") {
if (content.toolName !== reflectionToolName) continue;
const payload = content.input as ReflectIOShape;
toolResultMap.set(payload.id, payload);
content.toolName = payload.name;
content.input = payload.input;
}
if (content.type === "tool-result") {
if (content.output.type !== "text") continue;
if (content.toolName !== reflectionToolName) continue;
const payload = toolResultMap.get(content.toolCallId);
if (!payload) continue;
content.toolName = payload.name;
content.output = {
type: "text",
value: JSON.stringify(payload.output),
};
}
}
}
if (message.role === "tool") {
for (let j = 0; j < message.content.length; j++) {
const content = message.content[j]!;
if (content.output.type !== "text") continue;
if (content.toolName !== reflectionToolName) continue;
const payload = toolResultMap.get(content.toolCallId);
if (!payload) continue;
content.toolName = payload.name;
content.output = {
type: "text",
value: JSON.stringify(payload.output),
};
}
}
}
return output;
}