
sanketshinde3001/mcp
Developer toolsЭта страница переведена автоматически. Технические детали смотрите в оригинале. Открыть оригинал
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.
Обзор
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: 1. : An MCP server that provides greeting-related tools, resources, and prompts. 2. : 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 transport mechanism. This project was built following these main phases: 1. : Creating the MCP service provider. 2. : Creating a script to consume the server's services. 3. : Running the client script, which launches the server and demonstrates communication. 4. : Discussing how to integrate the server with existing MCP clients like VS Code extensions. Before you begin, ensure you have the following installed: * Node.js (v16 or higher recommended) * npm (usually included with Node.
README
Показан фрагмент оригинального 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.
- Imported required modules (
Установка
npx @modelcontextprotocol/inspector node /path/to/my-mcp-greeter-server/build/index.js