Skip to content
Merged
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
22 changes: 14 additions & 8 deletions lib/chat/setupToolsForRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ToolSet> {
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)
Expand Down
Loading