sampleLLM
Generate text samples from a large language model by providing a prompt and specifying the maximum token limit. Part of the MCP Elicitations Demo Server for dynamic user input collection.
Instructions
Samples from an LLM using MCP's sampling feature
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxTokens | No | Maximum number of tokens to generate | |
| prompt | Yes | The prompt to send to the LLM |
Implementation Reference
- src/tools/tool-sample-llm.ts:6-16 (schema)Zod schema defining the input parameters for the sampleLLM tool: required prompt, optional maxTokens (default 100), and optional systemPrompt.const SampleLLMSchema = z.object({ prompt: z.string().describe("The prompt to send to the LLM"), maxTokens: z .number() .default(100) .describe("Maximum number of tokens to generate"), systemPrompt: z .string() .optional() .describe("System prompt to guide the LLM's behavior"), });
- src/tools/tool-sample-llm.ts:22-41 (handler)The handler function for the sampleLLM tool. Validates input using SampleLLMSchema, performs LLM text sampling via requestTextSampling, and returns a text content block with the result.handler: async (args: any, request: any, server: Server) => { const validatedArgs = SampleLLMSchema.parse(args); const { prompt, maxTokens, systemPrompt } = validatedArgs; const result = await requestTextSampling( prompt, systemPrompt, maxTokens, server, undefined // modelPreferences ); return { content: [ { type: "text" as const, text: `LLM sampling result: ${result.content.text}`, }, ], }; },
- src/tools/index.ts:22-37 (registration)The sampleLlmTool is registered by being included in the allTools array. This array is used by getTools() to list tools and getToolHandler() to dispatch calls in the MCP server's setupTools function.const allTools = [ echoTool, addTool, longRunningOperationTool, printEnvTool, sampleLlmTool, sampleWithPreferencesTool, sampleMultimodalTool, sampleConversationTool, sampleAdvancedTool, getTinyImageTool, annotatedMessageTool, getResourceReferenceTool, elicitationTool, getResourceLinksTool, ];