MCP Server
A Model Context Protocol server that provides greeting tools, resources, and prompts, demonstrating client-server interaction using TypeScript.
README
MCP Server and Client Example (TypeScript)
This project demonstrates the creation and interaction of a simple Model Context Protocol (MCP) server and a standalone MCP client using TypeScript and the @modelcontextprotocol/sdk.
The setup includes:
my-mcp-greeter-server: An MCP server that provides greeting-related tools, resources, and prompts.my-mcp-client-script: A simple command-line client script that launches the server, connects to it, and interacts with its capabilities programmatically.
Communication between the client and server in this example uses the stdio (standard input/output) transport mechanism.
Overview of the Process Followed
This project was built following these main phases:
- Server Development: Creating the MCP service provider.
- Client Development: Creating a script to consume the server's services.
- Testing & Interaction: Running the client script, which launches the server and demonstrates communication.
- (Optional) Integration: Discussing how to integrate the server with existing MCP clients like VS Code extensions.
Prerequisites
Before you begin, ensure you have the following installed:
- Node.js (v16 or higher recommended)
- npm (usually included with Node.js)
- A text editor or IDE (like VS Code)
npx(usually included with npm) - useful for testing with MCP Inspector.
Phase 1: Building the MCP Server (my-mcp-greeter-server)
-
Project Setup:
- Created the directory
my-mcp-greeter-server. - Initialized an npm project:
npm init -y. - Installed necessary dependencies:
npm install @modelcontextprotocol/sdk zod. - Installed development dependencies:
npm install -D typescript @types/node. - Initialized TypeScript configuration:
npx tsc --init. - Configured
tsconfig.json(setting"module": "Node16","target": "ES2022","outDir": "./build","rootDir": "./src", etc.). - Updated
package.jsonto include"type": "module"and addedbuild/startscripts. - Created the source file
src/index.ts.
- Created the directory
-
Server Implementation (
src/index.ts):- Imported required modules (
McpServer,StdioServerTransport,z). - Defined constants for server
nameandversion. - Instantiated
McpServer, passing the name, version, and declaring its capabilities (tools, resources, prompts). - Defined a Tool (
greet): Usedserver.tool()to create a function callable by clients. Included a description, defined input parameters with Zod (name,politeness), and implemented the handler to return a personalized greeting string. - Defined a Resource (
server-info): Usedserver.resource()to expose static data. Provided a unique URI (info://greeter/about) and implemented the handler to return the server's name and version. - Defined a Prompt (
suggest-greeting): Usedserver.prompt()to create a reusable interaction template. Included a description and implemented the handler to return a predefined set of user/assistant messages to guide an LLM interaction. - Used Stdio Transport: Instantiated
StdioServerTransportas the communication method. - Connected: Called
await server.connect(transport)to make the server ready. - Logging: Added
console.errorstatements for visibility during execution, especially important for stdio transport where stdout is used for protocol messages. - Kept Alive: Ensured the Node.js process didn't exit immediately after connection.
- Imported required modules (
-
Building & Fixing:
- Ran
npm run buildto compile TypeScript to JavaScript in thebuilddirectory. - Fixed a TypeScript error related to accessing server version directly, opting to use predefined constants instead.
- Ran
Phase 2: Building the MCP Client Script (my-mcp-client-script)
-
Project Setup:
- Created a separate directory
my-mcp-client-script. - Initialized an npm project:
npm init -y. - Installed necessary dependencies:
npm install @modelcontextprotocol/sdk. - Installed development dependencies:
npm install -D typescript @types/node. - Initialized and configured
tsconfig.jsonsimilarly to the server project. - Updated
package.jsonwith"type": "module"andbuild/startscripts. - Created the source file
src/client-script.ts.
- Created a separate directory
-
Client Implementation (
src/client-script.ts):- Imported required modules (
Client,StdioClientTransport,path,url). - Determined Server Path: Calculated the path to the server's compiled
index.jsfile (relative or absolute). - Configured Stdio Transport: Instantiated
StdioClientTransport, providing thecommand(node) andargs(the path to the server script). This configuration is key, as the client transport launches the server process. - Instantiated Client: Created a
Clientinstance, giving it an identity and declaring its intent to use tools and resources. - Connected: Called
await client.connect(transport), which launched the server process and established the MCP connection over its stdio streams. - Interacted with Server:
- Called the
greettool usingawait client.callTool(). - Read the
server-inforesource usingawait client.readResource(). - Fetched the
suggest-greetingprompt usingawait client.getPrompt().
- Called the
- Logged Results: Used
console.logto display the responses received from the server. - Closed Connection: Used
await client.close()in afinallyblock to cleanly shut down the connection and terminate the server process.
- Imported required modules (
Phase 3: Building and Running
-
Build Both Projects:
cd my-mcp-greeter-server && npm run buildcd ../my-mcp-client-script && npm run build
-
Run the Client:
cd my-mcp-client-scriptnpm run start(ornode build/client-script.js)- Observed the interleaved output from both the client (
console.log) and the server (console.error), confirming successful communication and execution of tools/resources/prompts.
Explanation of Roles
-
The Server (
GreeterServer):- Provides Services: Exposes specific capabilities (greeting tool, server info, prompt template).
- Passive Listener (in stdio): Waits for a client to connect via its standard streams.
- Executes Logic: Runs the code associated with a tool/resource/prompt when requested by the client.
- Sends Results: Formats results according to MCP specs and sends them back to the client.
-
The Client (
client-script.ts):- Consumes Services: Uses the capabilities offered by the server.
- Initiator (in stdio): Launches the server process and establishes the connection.
- Sends Requests: Decides which tool to call, resource to read, or prompt to get, and sends the appropriate MCP request.
- Receives Results: Processes the responses sent back by the server.
- Controls Flow: Manages the sequence of interactions and decides when to close the connection.
Testing the Server Interactively
While the client script tests the programmatic interaction, you can test the server's capabilities individually using the MCP Inspector:
# Make sure the server is NOT already running
# Replace /path/to/... with the actual absolute path
npx @modelcontextprotocol/inspector node /path/to/my-mcp-greeter-server/build/index.js
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.