How to Set Up MCP in Custom Plugins: A Complete Guide to Model Context Protocol Integration

To set up MCP in custom plugins, create an MCP client using @ai-sdk/mcp to discover tools from a remote server, or implement your own MCP server using the McpAgent class to expose custom capabilities via Streamable HTTP transport.

The Model Context Protocol (MCP) standardizes how AI agents discover and invoke tools without hard-coding API details. In the openai/plugins repository, MCP integration appears in three architectural layers that enable seamless tool discovery and execution. This protocol handles authentication automatically and uses stateless transport, allowing agents to perform operations without exposing credentials.

Understanding MCP Architecture in OpenAI Plugins

The MCP implementation in OpenAI Plugins operates across three complementary layers, each documented in specific skill files within the repository.

MCP Client Integration

The client layer, documented in plugins/vercel/skills/ai-sdk/SKILL.md, demonstrates how agents create a client using @ai-sdk/mcp that discovers tools from a remote MCP server. The client calls mcpClient.tools() to retrieve tool definitions including name, input schema, and output schema, then validates arguments against Zod schemas before forwarding requests.

MCP Server Exposure

Vercel provides a built-in MCP server at https://mcp.vercel.com that proxies REST endpoints as discoverable tools. According to plugins/vercel/skills/vercel-api/SKILL.md, this server exposes categories like project lists, deployment logs, and environment variables through standardized JSON schemas.

MCP Server Implementation

For custom domains, developers can implement their own MCP servers using the McpAgent class. The plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md file provides boilerplate for hosting these servers on Cloudflare Workers, enabling any API to become MCP-compatible.

Setting Up an MCP Client for Tool Discovery

To consume MCP-exposed capabilities, initialize a client pointing to a remote MCP server. The following pattern from plugins/vercel/skills/ai-sdk/SKILL.md demonstrates connecting to Vercel's hosted server:

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

// Initialize the MCP client with Streamable HTTP transport
const mcpClient = await createMCPClient({
  transport: {
    type: "streamable-http",
    url: "https://mcp.vercel.com",
  },
});

// Discover available tools from the server
const tools = await mcpClient.tools();

// Pass tools to the LLM for automatic selection
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);

The zero-token-management feature means @ai-sdk/mcp handles OAuth refreshing internally, allowing read-only queries without credential exposure.

Building a Custom MCP Server

To expose custom tools, extend the McpAgent class and register tools using the server.tool() method. The implementation in plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md shows how to define tools with Zod 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) }],
        };
      }
    );
  }
}

Each tool requires three parameters: the tool name, a Zod schema for input validation, and an async implementation function returning a structured content array.

Deploying and Connecting to Your MCP Server

Expose your custom server via Streamable HTTP transport by creating a fetch handler. According to the Cloudflare skill file, route MCP requests to the agent's HTTP handler:

// 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 using wrangler deploy. Clients can then connect to your custom server:

const client = await createMCPClient({
  transport: { 
    type: "streamable-http", 
    url: "https://my-worker.workers.dev/mcp" 
  },
});

const tools = await client.tools(); // Discovers 'add' and 'get_weather'

Auditing Projects with MCP Tools

The Vercel MCP server enables multi-step audits without manual scripting. As documented in plugins/vercel/skills/vercel-api/SKILL.md (lines 38-48), the available tool categories allow complex orchestration:

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);

The model receives the tool catalog and autonomously selects the appropriate tools for each subtask, validating arguments against the server's exposed schemas.

Summary

  • MCP client setup requires installing @ai-sdk/mcp and calling createMCPClient() with a streamable-http transport configuration.
  • Tool discovery happens through mcpClient.tools(), which returns schema-validated tool definitions that LLMs can use for automatic selection.
  • Custom server implementation extends McpAgent and registers tools via server.tool() with Zod schemas for type-safe validation.
  • Key source files include plugins/vercel/skills/ai-sdk/SKILL.md for client patterns and plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md for server implementation.
  • Transport layer uses stateless Streamable HTTP (SSE-style), eliminating token management overhead through automatic OAuth handling.

Frequently Asked Questions

What is the Model Context Protocol (MCP) in OpenAI Plugins?

The Model Context Protocol is a standardized interface that allows AI agents to discover and invoke external tools without hard-coding API specifications. According to the OpenAI Plugins repository, MCP enables zero-token-management tool calling where authentication and schema validation happen automatically through the @ai-sdk/mcp package.

How do I authenticate with an MCP server without managing tokens?

The @ai-sdk/mcp client handles OAuth authentication internally, automatically refreshing tokens as needed. As implemented in the Vercel plugin (plugins/vercel/skills/vercel-api/SKILL.md), this allows agents to perform read-only queries against projects and logs without exposing or manually managing API credentials in the client code.

Can I expose my existing REST API as an MCP server?

Yes, by implementing the McpAgent class and registering your endpoints as tools using server.tool(). The Cloudflare Workers example in plugins/cloudflare/skills/building-mcp-server-on-cloudflare/SKILL.md demonstrates wrapping external APIs (like weather services) with Zod schemas, allowing any existing REST endpoint to become discoverable by MCP clients.

What transport protocol does MCP use for communication?

MCP uses Streamable HTTP transport, an SSE-style stateless protocol. The client configuration specifies type: "streamable-http" when calling createMCPClient(), and servers expose endpoints via serveStreamableHTTP() as shown in both the Vercel client examples and Cloudflare server implementations.

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 →