Skip to content

Add support for Sampling #82

@dclark27

Description

@dclark27

Sampling is a feature supported by both the MCP Protocol writ large and the modelcontextprotocol/typescript-sdk.

Currently, using methods like server.server.createMessage result in a timeout. Please either add support for sampling, or return early in the mcp-api-handler that the tool is not supported. The Inspector never gets notified of a request for sampling.

Sampling and Elicitation are at the heart of the MCP server I am building, so support for these 2 features would be so greatly appreciated! Also happy to create a PR for this.

Steps to reproduce

import { createMcpHandler } from 'mcp-handler';
import z from 'zod';

const handler = createMcpHandler(
	(server) => {
		server.registerTool(
			'summarize',
			{
				description: 'Summarize any text using an LLM',
				inputSchema: {
					text: z.string().describe('Text to summarize'),
				},
			},
			async ({ text }) => {
				// Call the LLM through MCP sampling
				const response = await server.server.createMessage({
					messages: [
						{
							role: 'user',
							content: {
								type: 'text',
								text: `Please summarize the following text concisely:\n\n${text}`,
							},
						},
					],
					maxTokens: 500,
				});

				return {
					content: [
						{
							type: 'text',
							text:
								response.content.type === 'text'
									? response.content.text
									: 'Unable to generate summary',
						},
					],
				};
			},
		);
	},
	{
		// Optional server options
	},
	{
		// Optional redis config
		redisUrl: process.env.REDIS_URL,
		basePath: '/api', // this needs to match where the [transport] is located.
		maxDuration: 60,
		verboseLogs: true,
	},
);
export { handler as GET, handler as POST };

Example video

Screen.Recording.2025-07-14.at.3.49.13.PM.mov

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions