Feat: add mcp support to chat

This commit is contained in:
Manoj K 2025-07-16 22:52:13 +05:30
parent f2b4a5f64a
commit 32379a0b0e
3 changed files with 14 additions and 20 deletions

View File

@ -262,6 +262,7 @@ export async function* run(
let guardLoop = 0;
let tools = {
...(await mcp.allTools()),
"core--progress_update": progressUpdateTool,
"core--search_memory": searchMemoryTool,
"core--add_memory": addMemoryTool,

View File

@ -37,9 +37,11 @@ export const chat = task({
const { previousHistory, ...otherData } = payload.context;
const { agents = [] } = payload.context;
// Initialise mcp
const mcp = new MCP();
await mcp.init();
await mcp.load(agents, { Authorization: `Bearer ${init?.token}` });
// Prepare context with additional metadata
const context = {

View File

@ -3,7 +3,7 @@ import { logger } from "@trigger.dev/sdk/v3";
import { jsonSchema, tool, type ToolSet } from "ai";
import { type MCPTool } from "./types";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
export class MCP {
private Client: any;
private clients: Record<string, any> = {};
@ -23,15 +23,14 @@ export class MCP {
return Client;
}
async load(agents: string[], mcpConfig: any) {
async load(agents: string[], headers: any) {
await Promise.all(
agents.map(async (agent) => {
const mcp = mcpConfig.mcpServers[agent];
return await this.connectToServer(agent, mcp.command, mcp.args, {
...mcp.env,
DATABASE_URL: mcp.env?.DATABASE_URL ?? "",
});
return await this.connectToServer(
agent,
`${process.env.BACKEND_HOST}/api/v1/mcp/${agent}`,
headers,
);
}),
);
}
@ -113,12 +112,7 @@ export class MCP {
return response;
}
async connectToServer(
name: string,
command: string,
args: string[],
env: any,
) {
async connectToServer(name: string, url: string, headers: any) {
try {
const client = new this.Client(
{
@ -130,12 +124,9 @@ export class MCP {
},
);
// Conf
// igure the transport for MCP server
const transport = new this.StdioTransport({
command,
args,
env,
// Configure the transport for MCP server
const transport = new StreamableHTTPClientTransport(new URL(url), {
requestInit: { headers },
});
// Connect to the MCP server