async function runAgentWithMCPClient()

in genai-function-calling/vercel-ai/mcp.js [58:80]


async function runAgentWithMCPClient(runAgent) {
    // MCP server is a subprocess, which doesn't inherit anything by default.
    // Minimally, we need to pass an argument to let the process know it is
    // running as a server. We also propagate any ENV or arguments to ensure
    // OpenTelemetry auto-instrumentation propagates to the child, such as
    // '-r @elastic/opentelemetry-node'. Don't do this with untrusted servers.
    const transport = new StdioClientTransport({
        command: process.execPath,
        args: [...process.execArgv, ...process.argv.slice(1), SERVER_ARG],
        env: process.env,
    });

    let client;
    try {
        client = await experimental_createMCPClient({transport});
        const tools = await client.tools();
        await runAgent(tools);
    } catch (error) {
        throw error;
    } finally {
        await client?.close();  // closing the client closes its transport
    }
}