The MCP Sampling AI Provider allows MCP servers to use AI models through the AI SDK by leveraging your existing client subscriptions (like VS Code Copilot). This transforms your MCP server into an agentic tool that can reason and make decisions, without requiring separate API keys or subscriptions.
The provider implements LanguageModelV2 by forwarding requests through MCP's sampling feature to the MCP client, offering unique advantages:
Server-Side AI Integration: Enable MCP servers to call language models directly through AI SDK's standard interface
No Direct Model Management: Forward AI requests to MCP clients, eliminating the need for multiple API keys
Model Flexibility: Let MCP clients decide which model to use based on preferences (cost, speed, intelligence)
Seamless AI SDK Compatibility: Support for generateText, streamText, generateObject, and experimental tool calling
Client Sampling Support: Add sampling capability to any MCP client with built-in helpers
Agentic Tools: Transform simple MCP tools into intelligent agents that can reason and make decisions
Must run inside an MCP Server - This is not a standalone AI SDK provider. It works by forwarding requests to the MCP client.
Client must support MCP Sampling - The connected MCP client must implement the sampling capability, or you can implement it yourself (see Client Sampling below).
To create an MCP Sampling provider instance, use the createMCPSamplingProvider function with your MCP server instance:
import { Server } from '@modelcontextprotocol/sdk/server/index.js';import { createMCPSamplingProvider } from '@mcpc-tech/mcp-sampling-ai-provider';// Create an MCP server with sampling capabilityconst server = new Server( { name: 'my-agent', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } },);const provider = createMCPSamplingProvider({ server });
Generate text using the MCP Sampling Provider in an MCP server tool:
import { Server } from '@modelcontextprotocol/sdk/server/index.js';import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';import { CallToolRequestSchema, ListToolsRequestSchema,} from '@modelcontextprotocol/sdk/types.js';import { createMCPSamplingProvider } from '@mcpc-tech/mcp-sampling-ai-provider';import { generateText } from 'ai';// Create MCP server with sampling capabilityconst server = new Server( { name: 'translator', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } },);// List available toolsserver.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'translate', description: 'Translate text to a target language using AI', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'The text to translate', }, target_lang: { type: 'string', description: 'The target language (e.g., "Spanish", "French")', }, }, required: ['text', 'target_lang'], }, }, ], };});// Register a translation tool that uses AIserver.setRequestHandler(CallToolRequestSchema, async request => { if (request.params.name === 'translate') { // Create provider from the server const provider = createMCPSamplingProvider({ server }); // Use AI SDK to translate text const { text } = await generateText({ model: provider.languageModel({ modelPreferences: { hints: [{ name: 'gpt-5-mini' }] }, }), prompt: `Translate to ${request.params.arguments?.target_lang}: ${request.params.arguments?.text}`, }); return { content: [{ type: 'text', text }] }; }});// Connect and startconst transport = new StdioServerTransport();await server.connect(transport);