new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Providers · all subjects

mcp-sampling/setup

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

MCP Sampling provider must run inside an MCP server

The MCP Sampling AI Provider is not a standalone AI SDK provider. It works by forwarding requests to the MCP client and must run inside an MCP server.

MCP Sampling requires client support for sampling

The connected MCP client must implement the sampling capability. VS Code with GitHub Copilot supports this. Claude Desktop and Cursor are tracking support. Alternatively, you can implement sampling yourself using setupClientSampling().

MCP Sampling provider installation

Install the MCP Sampling provider using: pnpm add @mcpc-tech/mcp-sampling-ai-provider, npm install @mcpc-tech/mcp-sampling-ai-provider, yarn add @mcpc-tech/mcp-sampling-ai-provider, bun add @mcpc-tech/mcp-sampling-ai-provider, or deno add jsr:@mcpc/mcp-sampling-ai-provider.

Create MCP Sampling provider instance

Use createMCPSamplingProvider function with an MCP server instance that has sampling capability enabled. The server is the only required configuration parameter.

generateText example with MCP Sampling provider

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'; const server = new Server( { name: 'translator', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } }, ); server.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'], }, }, ], }; }); server.setRequestHandler(CallToolRequestSchema, async request => { if (request.params.name === 'translate') { const provider = createMCPSamplingProvider({ server }); 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 }] }; } }); const transport = new StdioServerTransport(); await server.connect(transport);

streamText example with MCP Sampling provider

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 { streamText } from 'ai'; const server = new Server( { name: 'ai-assistant', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } }, ); server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'generate-story', description: 'Generate a story or poem using AI', inputSchema: { type: 'object', properties: {}, }, }, ], }; }); server.setRequestHandler(CallToolRequestSchema, async request => { if (request.params.name === 'generate-story') { const provider = createMCPSamplingProvider({ server }); const result = streamText({ model: provider.languageModel({ modelPreferences: { hints: [{ name: 'gpt-5-mini' }], speedPriority: 0.9, }, }), prompt: 'Write a short poem about coding.', }); const text = await result.text; return { content: [{ type: 'text', text }] }; } }); const transport = new StdioServerTransport(); await server.connect(transport);

Structured output example with MCP Sampling provider

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, Output } from 'ai'; import { z } from 'zod'; const server = new Server( { name: 'recipe-generator', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } }, ); server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'generate-recipe', description: 'Generate a recipe using AI', inputSchema: { type: 'object', properties: {}, }, }, ], }; }); server.setRequestHandler(CallToolRequestSchema, async request => { if (request.params.name === 'generate-recipe') { const provider = createMCPSamplingProvider({ server }); const recipeSchema = z.object({ recipe: z.object({ name: z.string(), cuisine: z.string(), ingredients: z.array(z.string()), steps: z.array(z.string()), }), }); const { output } = await generateText({ model: provider.languageModel({ modelPreferences: { hints: [{ name: 'gpt-5-mini' }] }, }), output: Output.object({ schema: recipeSchema }), prompt: 'Generate a delicious lasagna recipe.', }); return { content: [{ type: 'text', text: JSON.stringify(output, null, 2) }], }; } }); const transport = new StdioServerTransport(); await server.connect(transport);

Tool calling example with MCP Sampling provider

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, isStepCount } from 'ai'; import { z } from 'zod'; const server = new Server( { name: 'weather-agent', version: '1.0.0' }, { capabilities: { sampling: {}, tools: {} } }, ); server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'ask-weather', description: 'Ask a weather-related question', inputSchema: { type: 'object', properties: { question: { type: 'string', description: 'The weather question to ask', }, }, }, }, ], }; }); server.setRequestHandler(CallToolRequestSchema, async request => { if (request.params.name === 'ask-weather') { const provider = createMCPSamplingProvider({ server }); const result = await generateText({ model: provider.languageModel({ modelPreferences: { hints: [{ name: 'gpt-5-mini' }] }, }), tools: { getWeather: { description: 'Get the weather for a location', inputSchema: z.object({ city: z.string().describe('The city name'), }), execute: async ({ city }) => { return `The weather in ${city} is sunny and 72°F`; }, }, }, prompt: request.params.arguments?.question || 'What is the weather in San Francisco?', stopWhen: isStepCount(5), }); return { content: [{ type: 'text', text: result.text }] }; } }); const transport = new StdioServerTransport(); await server.connect(transport);

Client sampling implementation for clients without native support

import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; import { convertAISDKFinishReasonToMCP, selectModelFromPreferences, setupClientSampling, } from '@mcpc-tech/mcp-sampling-ai-provider'; import { generateText } from 'ai'; const client = new Client( { name: 'my-client', version: '1.0.0' }, { capabilities: { sampling: {} } }, ); setupClientSampling(client, { handler: async params => { const modelId = selectModelFromPreferences(params.modelPreferences, { hints: { 'gpt-5': 'openai/gpt-5-mini', 'gpt-mini': 'openai/gpt-5-mini', }, priorities: { speed: 'openai/gpt-5-mini', intelligence: 'openai/gpt-5-mini', }, default: 'openai/gpt-5-mini', }); const result = await generateText({ model: modelId, messages: params.messages, }); return { model: modelId, role: 'assistant', content: { type: 'text', text: result.text }, stopReason: convertAISDKFinishReasonToMCP(result.finishReason), }; }, }); const transport = new StdioClientTransport({ command: 'npx', args: ['-y', 'example_mcp_server.ts'], }); await client.connect(transport);

Give your agent this brain