MCPサーバーは多少の推奨規格があるものの、仕組み自体は単なるAPIです。
AIエージェントを使って10分で作成してしまいましょう。
ゴール
- Node.js + TypeScript で最小構成の MCP サーバーを作成
-
helloツールを 1 つだけ公開 - 入力内容に関わらず常に
"hello"を返す
ディレクトリ構成
mcp-hello/
├─ package.json
├─ tsconfig.json
└─ src/
└─ index.ts
package.json
{
"name": "mcp-hello",
"version": "0.1.0",
"type": "module",
"private": true,
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "node dist/index.js",
"dev": "tsx src/index.ts"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.0.0"
},
"devDependencies": {
"tsx": "^4.19.0",
"typescript": "^5.5.0"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "Bundler",
"strict": true,
"outDir": "dist",
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"]
}
src/index.ts
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { Tool } from "@modelcontextprotocol/sdk/types.js";
/** サーバー初期化 */
const server = new Server(
{
name: "mcp-hello",
version: "0.1.0"
},
{
capabilities: {
tools: {}
}
}
);
/** hello ツール定義 */
const helloTool: Tool = {
name: "hello",
description: "任意の入力に対し常に 'hello' を返す最小ツール",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "任意の文字列(未使用)" }
},
additionalProperties: true
}
};
/** ツール登録 */
server.tool(helloTool, async (_args, _ctx) => {
return {
// 何か処理を追加するならここですね
// 例1: プロジェクト管理ツールAPIを使って過去の同様の問題を検索する
// 入力: キーワード、出力: issueのコンテキスト
// 例2: コンパイラーを通してコンパイルが成功するか確認
// 入力: syntax、出力: OKメッセージ or Errorメッセージ
// ・・など
content: [{ type: "text", text: "hello" }]
};
});
/** stdio で接続開始 */
const transport = new StdioServerTransport();
await server.connect(transport);
起動方法
# 依存インストール
npm install
# 開発モード(ホット実行)
npm run dev
# またはビルドして実行
npm run build
npm start
使い方
Claude Code (.claude.json)
{
"mcpServers": {
"hello": {
"command": "node",
"args": ["/home/{user名}/work/mcp-hello/dist/index.js"],
"env": {
"MCP_STDIO": "1"
}
}
}
}
Cursor (.cursor/mcp.json)
{
"mcpServers": {
"hello": {
"command": "node",
"args": ["/home/{user名}/work/mcp-hello/dist/index.js"]
}
}
}
Serena (serena.config.json)
{
"mcpServers": {
"hello": {
"command": "node",
"args": ["/home/{user名}/work/mcp-hello/dist/index.js"]
}
}
}
Codex (.codex)
[mcp_servers.hello]
command = "node"
args = ["/home/{user名}/work/mcp-hello/dist/index.js"]
env.MCP_STDIO = "1"
Cline (cline.json)
{
"mcpServers": {
"hello": {
"command": "node",
"args": ["/home/{user名}/work/mcp-hello/dist/index.js"]
}
}
}