How to Use Model Context Protocol (MCP) with OpenAI Plugin Integration

Use the @ai-sdk/mcp package to create a client that discovers tools from an MCP server, then pass those tools to generateText or streamText to let the model invoke remote APIs without hard-coded endpoints.

The Model Context Protocol (MCP) standardizes how AI agents discover and invoke external tools through schema-validated JSON. Within the openai/plugins repository, MCP surfaces across three complementary layers—client integration, hosted server exposure, and custom server implementation—enabling seamless OpenAI plugin integration without manual token management.

What Is the Model Context Protocol (MCP)?

MCP is a standardized protocol that allows AI agents to interact with remote tools through a consistent interface. Instead of hard-coding API calls, agents use an MCP client to discover available tools, validate arguments against Zod schemas, and invoke functions exposed by an MCP server over a stateless Streamable HTTP transport.

Architectural Overview of MCP in OpenAI Plugins

The openai/plugins repository implements MCP across three architectural layers:

MCP Client Integration

Agents use the @ai-sdk/mcp package to instantiate a client that connects to a remote MCP server. The client retrieves tool definitions (name, input schema, output schema) and handles OAuth token refresh automatically. This pattern is documented in plugins/vercel/skills/ai-sdk/SKILL.md.

MCP Server Exposure

The Vercel plugin ships with a built-in MCP server hosted at https://mcp.vercel.com. This server proxies Vercel’s REST endpoints—projects, deployments, logs, and domains—as discoverable tools. Connection details and available tool categories are defined in plugins/vercel/skills/vercel-api/SKILL.md.

MCP Server Implementation

Developers can author custom MCP servers using Cloudflare Workers. The plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md file provides boilerplate code for registering tools with Zod schemas and exposing them via Streamable HTTP endpoints.

Implementing MCP Client Integration

To integrate MCP into your agent, initialize a client, fetch the tool list, and pass it to your LLM generation function. The model then handles tool selection and invocation automatically.

import { generateText } from "ai";
import { createMCPClient } from "@ai-sdk/mcp";

// Initialize the MCP client pointing to Vercel's hosted endpoint
const mcpClient = await createMCPClient({
  transport: {
    type: "streamable-http",
    url: "https://mcp.vercel.com",
  },
});

// Discover available tools (projects, logs, deployments, etc.)
const tools = await mcpClient.tools();

// Pass tools to the LLM—the model selects the appropriate tool
const result = await generateText({
  model: "openai/gpt-4o-mini",
  tools,
  prompt: "Show me the last three deployments for the project 'my-site'.",
});

await mcpClient.close();
console.log(result.output);

This four-step flow—Discover, Select, Invoke, and Result—eliminates the need to manually manage API endpoints or authentication tokens. The MCP client validates arguments against the server's Zod schema before forwarding requests via SSE-style HTTP transport.

Building Custom MCP Servers

For domain-specific functionality, implement an MCP server using Cloudflare Workers. Extend the McpAgent class to register tools with typed schemas.

// src/mcp.ts
import { McpAgent } from "agents/mcp";
import { z } from "zod";

export class MyMCP extends McpAgent {
  server = new Server({ name: "my-mcp", version: "1.0.0" });

  async init() {
    // Register a simple arithmetic tool
    this.server.tool(
      "add",
      { a: z.number(), b: z.number() },
      async ({ a, b }) => ({
        content: [{ type: "text", text: String(a + b) }],
      })
    );

    // Register an external API tool
    this.server.tool(
      "get_weather",
      { city: z.string() },
      async ({ city }) => {
        const res = await fetch(`https://api.weather.com/${city}`);
        const data = await res.json();
        return {
          content: [{ type: "text", text: JSON.stringify(data) }],
        };
      }
    );
  }
}

Expose the server through a Streamable HTTP endpoint in your worker entry point:

// src/index.ts
import { MyMCP } from "./mcp";

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    const url = new URL(request.url);
    if (url.pathname === "/mcp") {
      return MyMCP.serveStreamableHTTP("/mcp").fetch(request, env, ctx);
    }
    return new Response("MCP Server ready", { status: 200 });
  },
};

export { MyMCP };

Deploy with wrangler deploy. Clients can then connect to https://my-worker.workers.dev/mcp to discover and invoke your custom tools.

Real-World Example: Auditing Vercel Projects

Combine multiple MCP tools to perform complex audits without scripting individual API calls. The following example orchestrates environment variable checks, domain verification, and log retrieval through a single prompt:

import { generateText } from "ai";
import { createMCPClient } from "@ai-sdk/mcp";

const client = await createMCPClient({
  transport: { type: "streamable-http", url: "https://mcp.vercel.com" },
});

const tools = await client.tools();

const audit = await generateText({
  model: "openai/gpt-4o",
  tools,
  prompt: `
    1. List the environment variables for project "my-app".
    2. Show any domains that are not verified.
    3. Pull the latest deployment logs.
  `,
});

await client.close();
console.log(audit.output);

According to plugins/vercel/skills/vercel-api/SKILL.md, the available tool categories include project management, deployment logs, and domain configuration, enabling the model to resolve multi-step queries autonomously.

Key Files and References

The following source files in the openai/plugins repository provide authoritative implementation details:

Summary

  • MCP client integration uses @ai-sdk/mcp to connect to remote servers via createMCPClient with Streamable HTTP transport.
  • Tool discovery happens through mcpClient.tools(), which returns schema-validated tool definitions for the LLM.
  • Zero-token management is handled internally by the MCP client, including automatic OAuth refresh.
  • Custom servers extend McpAgent to register tools with Zod schemas and expose them through Cloudflare Workers.
  • Vercel's MCP server at https://mcp.vercel.com provides immediate access to projects, deployments, and logs without custom API code.

Frequently Asked Questions

What transport protocol does MCP use?

MCP uses Streamable HTTP, an SSE-style transport that maintains stateless connections between the client and server. This implementation appears in both the Vercel client examples (plugins/vercel/skills/ai-sdk/SKILL.md) and the Cloudflare server boilerplate (plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md).

How does the MCP client handle authentication?

The @ai-sdk/mcp package manages OAuth flows internally, automatically refreshing tokens without exposing credentials to the agent code. This zero-token-management approach allows agents to perform read-only queries safely.

Can I use MCP with any LLM model?

Yes. Once you retrieve tools via mcpClient.tools(), you can pass them to any AI SDK-compatible generation function such as generateText or streamText. The model receives the tool schemas and determines which tool to invoke based on the user prompt.

What is the difference between MCP client and server?

The MCP client (implemented with @ai-sdk/mcp) discovers and invokes tools, while the MCP server exposes those tools through defined endpoints. In the openai/plugins repository, Vercel provides a hosted server, while Cloudflare documentation shows how to build custom servers that clients can consume.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →