Many official platforms have now developed their own MCP services, such as GitLab, Alipay, and Amap. If you have development capabilities, you can also build your own MCP service. This article will teach you how to do just that. Let’s first understand how an MCP service works.
Transmission Methods
MCP services support two transmission methods: STDIO and SSE.
- STDIO uses standard input/output streams for data exchange.
- SSE (Server-Sent Events) is based on HTTP for one-way communication, where the server pushes data to the client.
Below, we’ll implement a simple STDIO-mode MCP service that triggers a custom addition tool when the large language model (LLM) identifies a need for addition operations.
Prerequisites
Ensure you have Node.js and npm installed. We’ll use the official MCP SDK to build the service.
Step 1: Initialize the Project and Install Dependencies
Use pnpm
to initialize the project, install dependencies, and declare type
as module
to enable ES Module syntax:
pnpm init pnpm add @modelcontextprotocol/sdk zod
Step 2: Create the MCP Server File (index.mjs
)
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // Create an MCP server const server = new McpServer({ name: "Demo", version: "1.0.0" }); // Add an addition tool server.tool("add", "Add two numbers", { a: z.number(), b: z.number() }, async ({ a, b }) => ({ content: [{ type: "text", text: String(a + b) }] }) ); async function main() { // Start receiving/sending messages via stdin/stdout const transport = new StdioServerTransport(); await server.connect(transport); } main();
- zod: A library for data validation, ensuring incoming parameters meet expected types.
- This service provides an
add
tool that accepts two numeric parametersa
andb
and returns their sum.
Step 3: Test the Tool with @modelcontextprotocol/inspector
Use the official inspector to test the service:
npx @modelcontextprotocol/inspector node index.mjs
- Visit
http://127.0.0.1:6274/
in your browser. - Click Connect to link to the MCP service.
- Use the List Tools and Add buttons to input parameters and test the tool (see screenshots below).
MCP Service Connection
Add Tool Test
Client Configuration: Using the MCP Service in Cursor
After debugging, configure the MCP service in the client (take Cursor as an example):
- Edit the configuration file and add:
{ "mcpServers": { "add-mcp": { "type": "stdio", "command": "npx", "args": ["-y", "D:\\kelen\\study\\add-mcp\\index.js"] } } }
- A green indicator confirms a successful connection.
MCP Service Configuration in Cursor
- Test with a加法 (addition) query. The LLM will automatically invoke the
add
tool and return results:
Automatic Tool Invocation in Cursor
Conclusion
This article demonstrates how to build a simple local MCP service using the MCP protocol and integrate it with an LLM. The core advantage of MCP lies in its ability to enable LLMs to automatically identify and invoke required tools, significantly enhancing efficiency. You can expand this framework to develop custom MCP tools for diverse use cases.