diff --git a/lib/chat/setupToolsForRequest.ts b/lib/chat/setupToolsForRequest.ts index f46e3371..a5fac3d0 100644 --- a/lib/chat/setupToolsForRequest.ts +++ b/lib/chat/setupToolsForRequest.ts @@ -2,16 +2,15 @@ import { ToolSet } from "ai"; import { filterExcludedTools } from "./filterExcludedTools"; import { ChatRequestBody } from "./validateChatRequest"; import { experimental_createMCPClient as createMCPClient } from "@ai-sdk/mcp"; -import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; +import { registerAllTools } from "@/lib/mcp/tools"; import { getGoogleSheetsTools } from "@/lib/agents/googleSheetsAgent"; -/** Base URL for the MCP server */ -const MCP_BASE_URL = process.env.MCP_BASE_URL || "https://recoup-api.vercel.app"; - /** * Sets up and filters tools for a chat request. * Aggregates tools from: - * - MCP client (remote tools via MCP protocol) + * - MCP server (in-process via in-memory transport, no HTTP overhead) * - Google Sheets (via Composio integration) * * @param body - The chat request body @@ -20,10 +19,17 @@ const MCP_BASE_URL = process.env.MCP_BASE_URL || "https://recoup-api.vercel.app" export async function setupToolsForRequest(body: ChatRequestBody): Promise { const { excludeTools } = body; - // Fetch MCP tools - const mcpClient = await createMCPClient({ - transport: new StreamableHTTPClientTransport(new URL("/mcp", MCP_BASE_URL)), + // Create in-memory MCP server and client (no HTTP call needed) + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); + + const server = new McpServer({ + name: "recoup-mcp", + version: "0.0.1", }); + registerAllTools(server); + await server.connect(serverTransport); + + const mcpClient = await createMCPClient({ transport: clientTransport }); const mcpClientTools = (await mcpClient.tools()) as ToolSet; // Fetch Google Sheets tools (authenticated tools or login tool)