Closed16
MCPサーバーを作ってみる

このスクラップについて
こちらの素晴らしい記事で MCP サーバーの作成に興味が湧いたので早速試してみよう。

公式ドキュメント

セットアップ
コマンド
cd ~/workspace
mkdir hello-mcp-server
cd hello-mcp-server
npm init -y
pnpm install @modelcontextprotocol/sdk zod
pnpm install -D @types/node typescript
mkdir src
touch src/index.ts

package.json
package.json(追記)
{
"type": "module",
"bin": {
"weather": "./build/index.js"
},
"scripts": {
"build": "tsc && chmod 755 build/index.js"
},
"files": [
"build"
],
}

tsconfig.json
コマンド
touch tsconfig.json
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

コーディング
src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "waether",
version: "1.0.0",
});
server.tool(
"double_number",
"与えられた数値を2倍にする",
{
num: z.number().describe("数値"),
},
({ num }) => ({
content: [
{
type: "text",
text: (num * 2).toString(),
},
],
}),
);
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Weather MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});

Biome が欲しい
コマンド
pnpm add --save-dev --save-exact @biomejs/biome
pnpm biome init

ビルド
コマンド
pnpm build

テスト実行
コマンド
node build/index.js
コンソール出力
Weather MCP Server running on stdio
標準入出力は MCP 用に使われているので info も error もログは標準エラー出力に表示するようにしているみたい。

Claude Desktop の設定
コマンド
code ~/Library/Application\ Support/Claude/claude_desktop_config.json
claude_desktop_config.json(例)
{
"mcpServers": {
"weather": {
"command": "node",
"args": [
"/Users/susukida/workspace/hello-mcp-server/build/index.js"
]
}
}
}

Claude Desktop での動作確認
エラーが発生してしまった
mcp-server-weather.log
2025-05-05T06:24:10.835Z [weather] [info] Initializing server...
2025-05-05T06:24:10.859Z [weather] [info] Server started and connected successfully
2025-05-05T06:24:10.894Z [weather] [info] Message from client: {"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"claude-ai","version":"0.1.0"}},"jsonrpc":"2.0","id":0}
/Users/susukida/workspace/hello-mcp-server/build/index.js:1
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:760:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
at internal/main/run_main_module.js:21:11
2025-05-05T06:24:11.107Z [weather] [info] Server transport closed
2025-05-05T06:24:11.108Z [weather] [info] Client transport closed
2025-05-05T06:24:11.108Z [weather] [info] Server transport closed unexpectedly, this is likely due to the process exiting early. If you are developing this MCP server you can add output to stderr (i.e. `console.error('...')` in JavaScript, `print('...', file=sys.stderr)` in python) and it will appear in this log.
2025-05-05T06:24:11.108Z [weather] [error] Server disconnected. For troubleshooting guidance, please visit our [debugging documentation](https://modelcontextprotocol.io/docs/tools/debugging) {"context":"connection"}
2025-05-05T06:24:11.108Z [weather] [info] Client transport closed

参考になりそう

無理矢理だが一応解決
claude_desktop_config.json(例)
{
"mcpServers": {
"weather": {
"command": "/Users/susukida/.nvm/versions/node/v20.15.1/bin/node",
"args": ["/Users/susukida/workspace/hello-mcp-server/build/index.js"]
}
}
}

解決
nvm の未使用バージョンをアンインストールしたら無事に解決した。
コマンド
nvm ls --no-colors | grep -o "v[0-9.]\+"

動作確認
プロンプト
double_number を使って 3 を 2 倍にした結果を表示してください
無事に結果が表示された

おわりに
MCP Server を通じて AI にいろいろなことをやってもらえそうなので夢が広がった。
API だけ用意しておいて UI にはチャットを使うというアプリもありかも知れないと思った。
このスクラップは4ヶ月前にクローズされました