MCP Server with Tool, Resource, Prompt, and Sampling
Enables LLMs to read and write local user data, generate fake users via sampling, and interact with structured prompts and resources.
README
MCP Server with Tool, Resource, Prompt, and Sampling
A full-featured Model Context Protocol (MCP) server built with Node.js and TypeScript. This project demonstrates how to implement and integrate the core capabilities of MCP into a single server, communicating over stdio.
🚀 Features
This server showcases the four main pillars of the Model Context Protocol:
- Resources (
users://all): Exposes local data to the LLM. It safely reads fromsrc/data/users.jsonusingnode:fsso the LLM can analyze existing user data. - Tools (
create-user): Allows the LLM to take action by securely writing new user data to the local file system with strict validation viazod. - Sampling (
create-random-user): Demonstrates server-to-client LLM requests. When triggered, the server uses thesampling/createMessageendpoint to ask the client LLM to generate fake user data, parses the response, and automatically saves it. - Prompts: Configured to support structured prompt templates that guide the LLM's interactions with the provided tools and resources.
🛠️ Prerequisites
- Node.js: v20 or higher (Tested on v24)
- Package Manager:
yarnornpm
📦 Installation
-
Clone the repository and navigate to the directory:
cd mcp-server-samplingInstall dependencies:
yarn install(Ensure @modelcontextprotocol/sdk and zod are installed).
Initialize the data file: Ensure you have an empty or valid JSON array in src/data/users.json:
JSON:🏗️ Build & Run
Because this project uses modern ES Modules (ESM) and is strictly typed, you must build the project before connecting it to an MCP client like VS Code.
Compile the TypeScript code to JavaScript:
Bash yarn build (This outputs the compiled code to the /build directory).
(Optional) Test the server locally using the MCP Inspector:
npx @modelcontextprotocol/inspector node build/server.js
🔌 VS Code Configuration
To connect this server to VS Code (or Cursor/Claude Desktop), you need to register it in your client's MCP settings.
For VS Code, update your configuration (usually found in your MCP settings file) to point to the compiled server.js file:
{
"mcp.servers": {
"my-mcp-server-sampling": {
"type": "stdio",
"command": "node",
"args": ["${workspaceFolder}/build/server.js"],
"cwd": "${workspaceFolder}"
}
}
}
⚠️ Important Troubleshooting Note:
Always remember to run yarn build after making any changes to src/server.ts. VS Code executes the compiled build/server.js file, so if you skip the build step, your changes will not be reflected in the IDE.
🐛 Common Pitfalls Avoided
ESM Imports: All @modelcontextprotocol/sdk imports include the .js extension to comply with strict Node.js ESM resolution.
Stdio Corruption: Uses console.error exclusively for logging to prevent corrupting the JSON-RPC stdout stream used for client-server communication.
Dynamic JSON Loading: Uses node:fs with absolute paths rather than import statements to reliably read users.json both in development (tsx) and production (node).
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.