Sample MCP Server

Sample MCP Server

A minimal MCP server for learning, exposing three tools: sayHello, addNumbers, and getCurrentTime.

Category
访问服务器

README

Sample MCP Server

This is a complete, minimal Model Context Protocol (MCP) server written in TypeScript. It runs locally, uses the official MCP TypeScript SDK, and exposes exactly three tools:

  • sayHello
  • addNumbers
  • getCurrentTime

The goal is learning. The code is intentionally small, commented, and direct.

What MCP Is

Model Context Protocol is a standard way for AI applications to connect to external tools and context.

Without MCP, every app invents its own custom way to call tools. With MCP, a host such as ChatGPT can talk to an MCP server using a shared protocol.

An MCP server can expose:

  • Tools: actions the model can call
  • Resources: data the model can read
  • Prompts: reusable prompt templates

This project exposes only tools.

How This Project Works

This server uses the local stdio transport.

That means ChatGPT starts the server as a child process and communicates with it through standard input and standard output:

  • ChatGPT writes JSON-RPC messages to the server's stdin.
  • The server reads those messages with the MCP SDK.
  • The SDK routes tool calls to our TypeScript functions.
  • The server writes JSON-RPC responses back to stdout.

There is no HTTP server, no Express app, no database, no authentication, and no cloud service.

How ChatGPT Discovers Tools

When ChatGPT connects to an MCP server, it performs an initialization handshake. After that, it asks the server which tools are available.

The MCP SDK answers with each tool's:

  • Name
  • Description
  • Input schema

For example, addNumbers tells ChatGPT that it needs two numeric inputs named a and b.

How Tool Execution Works

When ChatGPT decides to call a tool, it sends a JSON-RPC request containing:

  • The tool name
  • The tool arguments
  • A request id so the response can be matched

The MCP SDK receives the request, validates the shape using the tool schema, and calls the matching TypeScript function.

The function returns an MCP tool result. In this project, results are text content blocks. addNumbers returns JSON as formatted text so it is easy to read.

JSON-RPC Message Flow

The message flow looks like this:

ChatGPT starts: node build/index.js
ChatGPT -> server: initialize
server -> ChatGPT: server name, version, capabilities
ChatGPT -> server: list tools
server -> ChatGPT: sayHello, addNumbers, getCurrentTime
ChatGPT -> server: call addNumbers with { "a": 2, "b": 3 }
server -> ChatGPT: { "result": 5 }

You usually do not write JSON-RPC code by hand. The SDK handles it.

Folder Structure

sample-mcp/
├── package.json
├── tsconfig.json
├── README.md
└── src/
    ├── index.ts
    ├── server.ts
    ├── tools/
    │   ├── hello.ts
    │   ├── math.ts
    │   └── time.ts
    └── types.ts

File Guide

package.json

Defines the project name, dependencies, and scripts.

Important scripts:

  • npm run build compiles TypeScript into build/.
  • npm start runs the compiled MCP server.

tsconfig.json

Configures TypeScript.

This project uses NodeNext module settings because the MCP SDK is designed for modern ES modules.

src/index.ts

The entry point.

It creates the server, creates the stdio transport, and connects them.

This file also catches startup errors and writes them to stderr. For stdio MCP servers, stdout must be reserved for JSON-RPC messages.

src/server.ts

Creates the McpServer instance and registers all tools.

Keeping this separate from index.ts makes the transport setup easy to understand.

src/tools/hello.ts

Registers the sayHello tool.

Input:

{
  "name": "Ada"
}

Output:

Hello Ada!

src/tools/math.ts

Registers the addNumbers tool.

Input:

{
  "a": 2,
  "b": 3
}

Output:

{
  "result": 5
}

src/tools/time.ts

Registers the getCurrentTime tool.

It accepts no input and returns the local date and time from your computer.

src/types.ts

Contains tiny helper functions for returning MCP tool results and formatting validation errors.

This keeps the tool files readable without adding a large abstraction layer.

Install Dependencies

From the project folder:

cd sample-mcp
npm install

Node.js 18 or newer is required.

Build the Server

npm run build

This compiles TypeScript from src/ into JavaScript in build/.

Run the Server

npm start

The server will appear to wait silently. That is normal.

An MCP stdio server does not print normal logs to stdout because stdout is reserved for JSON-RPC messages.

Connect It To ChatGPT

Use ChatGPT's MCP connector settings and add a local stdio server.

Use a command like this:

node D:\github\mcp\sample-mcp\build\index.js

If your path contains spaces, wrap it in quotes.

ChatGPT will start the command, initialize the MCP connection, discover the three tools, and call them when useful.

How To Add A New Tool

  1. Create a new file in src/tools/.
  2. Define a Zod input schema.
  3. Export a function that receives server: McpServer.
  4. Call server.registerTool(...).
  5. Import and call your registration function in src/server.ts.

Example shape:

export function registerExampleTool(server: McpServer): void {
  server.registerTool(
    "exampleTool",
    {
      title: "Example Tool",
      description: "Explain what this tool does.",
      inputSchema: {
        value: z.string(),
      },
    },
    async (input) => {
      const parsed = schema.parse(input);
      return textResult(parsed.value);
    },
  );
}

Common Errors And Fixes

node is not recognized

Install Node.js 18 or newer, then reopen your terminal.

Check your version:

node --version

Cannot find module

Dependencies are missing.

Run:

npm install

TypeScript build fails

Run the build from inside the project folder:

cd sample-mcp
npm run build

ChatGPT does not show the tools

Make sure you built the project first:

npm run build

Then make sure ChatGPT points to the compiled file:

node D:\github\mcp\sample-mcp\build\index.js

The server prints nothing

That is expected for stdio MCP servers.

Normal text on stdout would break JSON-RPC communication, so this server only writes startup errors to stderr.

Invalid input

The tools use Zod validation. If a value has the wrong type, the tool returns a meaningful error result instead of crashing the server.

Tools Included

sayHello

Input:

{
  "name": "Grace"
}

Output:

Hello Grace!

addNumbers

Input:

{
  "a": 10,
  "b": 5
}

Output:

{
  "result": 15
}

getCurrentTime

Input:

{}

Output:

7/21/2026, 10:30:00 PM

The exact format depends on your operating system locale.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选