自分の細々とした自動化ツールをAIエージェントも使えるようにするためのMCPサーバ private https://github.com/nishio/my-tools public https://github.com/nishio/my-tools-mcp-server-public-preview
2025-02-03
MCPサーバを作る
$ mkdir my-tools
$ code my-tools
add a tool
ローカルのPythonスクリプトを呼び出す実験として、呼び出したら現在時刻を返すものを作ろうと思う
MCPサーバーを作成
$ npx @modelcontextprotocol/create-server time-server
MCPサーバーはすでにビルドされているとのことですので、次のステップはこのサーバーにPythonスクリプトを呼び出すツールを追加することです。サーバーの設定ファイルを編集して、Pythonスクリプトを呼び出すツールを追加します。 index.ts
{
name: "get_current_time",
description: "Get the current time",
inputSchema: {
type: "object",
properties: {},
required: []
}
}
なるほど index.ts
server.setRequestHandler(CallToolRequestSchema, async (request) => {
switch (request.params.name) {
case "get_current_time": {
const { execSync } = require('node:child_process');
const output = execSync('python3 current_time.py').toString().trim();
return {
content: [{
type: "text",
text: `Current time is: ${output}`
}]
};
}
default:
throw new Error("Unknown tool");
}
});
なるほど
:
{
"mcpServers": {
"time-server": {
"command": "node",
"args": ["/Users/nishio/my-tools/time-server/build/index.js"],
"env": {}
}
}
}
current_time.pyスクリプトが見つからないというエラーが発生しています。これは、スクリプトのパスが正しく指定されていないことが原因です。execSyncでスクリプトを実行する際に、正しいパスを指定する必要があります。time-server/src/index.tsファイルを修正して、current_time.pyの正しいパスを指定します。
できた!
Claude Desktop
できたできた
理屈はわかったので今後どうするかを考える 細々としたツールを作るたびに新しいサーバを作って設定するのは何だから、my-toolsというサーバに工具箱的に色々入れてる感じにしたい
$ npx @modelcontextprotocol/create-server mcp-server
で、time-serverの時間取得機能をmcp-serverに移す
できた。
このMCPサーバを汎用の「他の人も使うもの」として公開してメンテナンスするより、自分専用のものにして「自分はDropboxのここに論文を入れてる」とかの知識をコードとして言語化していった方が「僕+AIエージェント」としての生産性は高くなると思う