The Model Context Protocol (MCP) is an open standard that allows your agents to connect securely to external tools and data sources. Think of MCP as a “universal connector” for AI — it standardizes how language models interact with various systems like databases, APIs, file systems, and custom tools.
MCP transforms your agents from isolated models into powerful assistants that can access real-time data, perform actions, and interact with your entire digital ecosystem through a single, standardized protocol.
1
Setting up the MCP Client
First, let’s install the necessary dependencies to work with MCP in your C1 application.
You’ll need to install the MCP client library and any specific MCP servers you want to use. For this example, we’ll use a filesystem MCP server.
Now let’s create the MCP client using the @modelcontextprotocol/sdk package.
This implementation connects to a filesystem MCP server and handles tool execution.
Create app/api/chat/mcp.ts:
Copy
import { Client } from "@modelcontextprotocol/sdk/client/index.js";import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";import OpenAI from "openai";export class MCPClient { private mcp: Client; private transport: StdioClientTransport | null = null; public tools: OpenAI.ChatCompletionTool[] = []; constructor() { this.mcp = new Client({ name: "c1-chat-mcp-client", version: "1.0.0" }); } async connect() { // Connect to filesystem MCP server (no authentication required) const command = "npx"; const args = [ "-y", "@modelcontextprotocol/server-filesystem@latest", process.cwd(), ]; this.transport = new StdioClientTransport({ command, args, }); await this.mcp.connect(this.transport); // List available tools from the MCP server const toolsResult = await this.mcp.listTools(); this.tools = toolsResult.tools.map((tool) => ({ type: "function" as const, function: { name: tool.name, description: tool.description, parameters: tool.inputSchema, }, })); } async runTool({ tool_call_id, name, args, }: { tool_call_id: string; name: string; args: Record<string, unknown>; }) { try { const result = await this.mcp.callTool({ name, arguments: args, }); return { tool_call_id, role: "tool" as const, content: JSON.stringify(result.content), }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; return { tool_call_id, role: "tool" as const, content: JSON.stringify({ error: `Tool call failed: ${errorMessage}`, }), }; } } async disconnect() { if (this.transport) { await this.transport.close(); } }}
3
Integrate MCP with your C1 agent
Now let’s update your chat route to use the streamlined MCP integration from the thesysdev examples. This approach uses OpenAI’s runTools method for automatic tool execution.