Learn about MCP resources and prompts sampling with easy-to-follow steps to create meaningful AI interactions. This guide explains how to set up MCP primitives like prompts and resources, and highlights prompts sampling for advanced interactions.
Overview of MCP Primitives
The Model Context Protocol (MCP) offers three key primitives that enhance user-model interactions:
- Prompts: Predefined templates for user-driven AI interactions, enabling users to insert custom text into the model's context window.
- Resources: Provide raw, structured data (e.g., database schemas) for integration into the context window or for generating embeddings.
- Tools: Allow models to invoke server-side actions autonomously.
Each primitive serves a unique purpose and contributes to building a seamless integration between servers, clients, and language models.
Setting Up MCP Prompts for AI Interactions
Follow these steps to define prompts for user-driven AI model interactions. We'll demonstrate this with an example of retrieving GitHub comments using TypeScript.
prerequisites
- Node.js installed (version 18 or higher).
- TypeScript setup in your project.
- Familiarity with MCP client-server architecture.
steps
Initialize a TypeScript Project
bashmkdir mcp-prompts-example cd mcp-prompts-example npm init -y npm install typescript --save-dev npx tsc --initInstall MCP Dependencies
bashnpm install @mcp/server @mcp/promptsDefine a Prompt Template
Create a file,
prompts.ts, with the following content:typescriptimport { createPrompt } from '@mcp/prompts'; export const fetchGitHubComments = createPrompt({ name: 'FetchGitHubComments', description: 'Fetches comments from a GitHub pull request.', parameters: { repo: { type: 'string', description: 'Repository name' }, pullRequestId: { type: 'number', description: 'Pull request ID' }, }, execute: async ({ repo, pullRequestId }) => { // Simulate fetching comments - replace with actual API calls as needed return `Fetching comments for PR ${pullRequestId} in repo ${repo}...`; }, });Integrate the Prompt into Your Server
Create an
index.tsfile and set up a simple MCP server using the prompt:typescriptimport { createServer } from '@mcp/server'; import { fetchGitHubComments } from './prompts'; const server = createServer({ name: 'GitHub Comment Fetcher', description: 'A server that fetches GitHub pull request comments.', prompts: [fetchGitHubComments], }); server.listen(3000, () => { console.log('MCP Server is running on http://localhost:3000'); });Test the Server
Run the server, then use an MCP client to invoke the
FetchGitHubCommentsprompt.bashnpx ts-node index.ts # MCP Server is running on http://localhost:3000
This demonstrates how to build and run a simple MCP prompt to fetch GitHub comments dynamically. Customize prompt parameters as needed for other use cases.
Implementing Resources for Data Exposure
Resources let servers expose structured data, which can then be added to the model's context or processed further. Here's how to expose a database schema as a resource:
prerequisites
- Node.js and TypeScript, as set up above.
- Basic knowledge of databases.
steps
Install PostgreSQL Driver
bashnpm install pgCreate a Resource for Database Schema
In a new file
resources.ts, define the resource:typescriptimport { createResource } from '@mcp/server'; export const databaseSchema = createResource({ name: 'DatabaseSchema', description: 'Provides the schema of a PostgreSQL database.', async fetch() { return { tables: [ { name: 'users', columns: ['id', 'name', 'email'] }, { name: 'orders', columns: ['id', 'user_id', 'total'] }, ], }; }, });Register the Resource in Your Server
Update
index.ts:typescriptimport { createServer } from '@mcp/server'; import { databaseSchema } from './resources'; const server = createServer({ name: 'Database Schema Viewer', description: 'A server that exposes database schema as a resource.', resources: [databaseSchema], }); server.listen(3000, () => { console.log('MCP Server is running on http://localhost:3000'); });Test Resource Exposure
Start the server and access the resource via an MCP client, adding it to the context window or using it for further processing.
bashnpx ts-node index.ts # MCP Server is running on http://localhost:3000
Understanding Prompts Sampling
Prompts sampling allows MCP servers to request completions on behalf of client-selected models. This offers users control over privacy, costs, and output preferences.
For example, instead of the server using its own API key for a completion, it delegates the task to the client, which interacts directly with its preferred model. This enables features like recursive requests, where one server can embed another server's capability to perform tasks. While this feature isn't widely supported yet, it shows promise for building advanced systems.
Best Practices for MCP Interaction Models
Choose the Right MCP Primitive
- Use Prompts for user-driven interactions involving context input or template suggestions.
- Use Resources when exposing structured or raw data for context or processing.
- Use Tools for model-driven actions that require server-side logic and automation.
What's Next for MCP Development
The MCP ecosystem is evolving to enable even richer interactions:
- Web-based MCP servers with OAuth 2.1 authentication.
- Streamable HTTP for scalable interactions.
- Developments in agent support, asynchronous tasks, and multimodal capabilities.
FAQ
What are MCP resources?
MCP resources expose raw or structured data from a server, which can be used in model interactions or for context enrichment. For example, resources can represent database schemas or other structured files that users or applications can integrate into their workflows.
How does prompts sampling work in MCP?
Prompts sampling allows an MCP server to delegate completion requests to the client's preferred model. This gives the user control over privacy, cost, and interaction quality, ensuring the client chooses its desired LLM for computation.
Can all MCP clients use prompts sampling?
Support for prompts sampling depends on the specific MCP client. As of October 2023, some clients implement it while others do not. Verify compatibility before implementing this feature in your servers.
Official reference: Model Context Protocol documentation.