Closed25
MCP サーバについて調べる

MCP Server を作ってみる
3種類ある
- Resources: File-like data that can be read by clients (like API responses or file contents)
- Tools: Functions that can be called by the LLM (with user approval)
- Prompts: Pre-written templates that help users accomplish specific tasks

weather.py を実装

Claude Desktop の config 編集
cursor ~/Library/Application\ Support/Claude/claude_desktop_config.json
以下のように
{
"mcpServers": {
"weather": {
"command": "/Users/(user.name)/.local/bin/uv",
"args": [
"--directory",
"/Users/(user.name)/work/mcp-server/weather",
"run",
"weather.py"
]
}
}
}

Claude Desktop を再起動

テスト
What’s the weather in Sacramento?

応答無い...

チャット閉じて開き直したら動き出した

I can help you check the weather forecast for Sacramento. Let me use the weather tool to get that information for you.
To do this, I'll need to use Sacramento's coordinates. Sacramento, California is located at approximately latitude 38.58 and longitude -121.49.
Here's the current weather forecast for Sacramento:
Today (Wednesday):
- High temperature: 70°F
- Mostly sunny
- Southwest wind 6 to 13 mph, with gusts up to 22 mph
- Temperatures will fall to around 68°F in the afternoon
Tonight:
- Low around 55°F
- Mostly cloudy
- South southwest wind 5 to 10 mph, with gusts up to 18 mph
Tomorrow (Thursday):
- High near 66°F
- Mostly cloudy
- 40% chance of rain showers after 11am
- South southwest wind 6 to 14 mph, with gusts up to 24 mph
Would you like me to check if there are any weather alerts for the area as well?

ログを見る
MCP サーバとのやりとりは ~/Library/Logs/Claude/mcp.log
にログが出力されている
2025-03-26T12:53:37.434Z [info] [weather] Message from client: {"method":"tools/call","params":{"name":"get_forecast","arguments":{"latitude":38.58,"longitude":-121.49}},"jsonrpc":"2.0","id":233}
2025-03-26T12:53:38.013Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":233,"result":{"content":[{"type":"text","text":"\nOvernight:\nTemperature: 56°F\nWind: 5 mph SSW\nForecast: Mostly clear. Low around 56, with temperatures rising to around 58 overnight. South southwest wind around 5 mph.\n\n---\n\nWednesday:\nTemperature: 70°F\nWind: 6 to 13 mph SW\nForecast: Mostly sunny. High near 70, with temperatures falling to around 68 in the afternoon. Southwest wind 6 to 13 mph, with gusts as high as 22 mph.\n\n---\n\nWednesday Night:\nTemperature: 55°F\nWind: 5 to 10 mph SSW\nForecast: Mostly cloudy, with a low around 55. South southwest wind 5 to 10 mph, with gusts as high as 18 mph.\n\n---\n\nThursday:\nTemperature: 66°F\nWind: 6 to 14 mph SSW\nForecast: A chance of rain showers after 11am. Mostly cloudy, with a high near 66. South southwest wind 6 to 14 mph, with gusts as high as 24 mph. Chance of precipitation is 40%.\n\n---\n\nThursday Night:\nTemperature: 52°F\nWind: 6 to 14 mph SSW\nForecast: A chance of rain showers. Mostly cloudy, with a low around 52. South southwest wind 6 to 14 mph, with gusts as high as 24 mph. Chance of precipitation is 40%.\n"}],"isError":false}}

テスト2
テキサスの現在の気象警報は?
日本語でもよしなに理解してAPI呼び出してくれた
かしこい
テキサス州の気象警報を確認します。
テキサス州では、現在いくつかの重大な気象警報が発令されています:
1. **洪水警報(フラッドウォッチ)**:
- 影響地域:テキサス南部の多くの郡(Zapata, Jim Hogg, Brooks, Kenedy, La Salle, Webb, Duval, Nueces, San Patricio, Aransas など)
- 期間:本日午後から金曜日の朝まで
- 詳細:豊富な湾岸湿気と遅く移動する暴風雨システムにより、非常に激しい降雨を伴う複数回の雨と雷雨が予想されています。過去数ヶ月の少雨による硬く乾いた土壌のため、この激しい雨がすぐに流れ出し鉄砲水を引き起こす可能性があります。
2. **洪水警報(フラッドウォッチ)**:
- 影響地域:テキサス南中部および南東部(Wharton, Jackson, Matagorda, Brazoria など)
- 期間:木曜日の朝から金曜日の午後まで
- 詳細:木曜日の朝早くから雨と嵐が予想されますが、日中は雨が小休止する可能性があります。激しい雨と鉄砲水の可能性は、木曜日の夕方から金曜日の朝にかけて最も高くなります。南東テキサスでは2.0〜5.0インチの降雨量が予測されていますが、警報地域内ではより高い5.0〜9.0インチの降雨量の可能性があります。
3. **洪水警告(フラッドワーニング)**:
- 影響地域:テキサス州Orange郡、Newton郡およびルイジアナ州の一部
- 期間:さらなる通知があるまで
- 詳細:Sabine川のDeweyville付近で軽度の洪水が発生しており、引き続き予測されています。川の水位は24.3フィートで、洪水レベルは24.0フィートです。
これらの警報は非常に深刻であり、特に影響を受ける地域では注意が必要です。最新の予報を監視し、フラッシュフラッド警告が発令された場合に備えて行動できるよう準備しておくことをお勧めします。

ログを確認
ログをみた感じ MCP サーバに対しては 素の API のレスポンスだけど、MCP client 側で受け取った結果をもとに日本語に翻訳して表示してくれてるらしい
2025-03-26T12:59:16.941Z [weather] [info] Message from client: {"method":"tools/call","params":{"name":"get_alerts","arguments":{"state":"TX"}},"jsonrpc":"2.0","id":290}
2025-03-26T12:59:17.234Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":290,"result":{"content":[{"type":"text","text":"\nEvent: Flood Watch\nArea: Zapata; Jim Hogg; Brooks; Inland Kenedy; Coastal Kenedy; Kenedy Island\nSeverity: Severe\nDescription: * WHAT...Flash flooding caused by excessive rainfall is possible.\n\n* WHERE...A portion of Deep South Texas, including the following\nareas, Brooks, Coastal Kenedy, Inland Kenedy, Jim Hogg, Kenedy\nIsland and Zapata.\n\n* WHEN...From this evening through Friday morning.\n\n* IMPACTS...Excessive runoff may result in flooding of rivers,\ncreeks, streams, and other low-lying and flood-prone locations.\nFlooding may occur in poor drainage and urban areas. Storm drains\nand ditches may become clogged with debris.\n\n* ADDITIONAL DETAILS...\n- Abundant gulf moisture combined with a slow moving storm\nsystem will bring the potential for multiple rounds of\nshowers and thunderstorms, some with very heavy rainfall.\nThere is the potential for rapid runoff where excessive\nrainfall occurs over areas that have seen extensive drought\nconditions.\n- http://www.weather.gov/safety/flood\nInstruction: You should monitor later forecasts and be prepared to take action\nshould Flash Flood Warnings be issued.\n\n---\n\nEvent: Flood Watch\nArea: La Salle; McMullen; Live Oak; Bee; Goliad; Victoria; Webb; Duval; Jim Wells; Inland Kleberg; Inland Nueces; Inland San Patricio; Coastal Aransas; Inland Refugio; Inland Calhoun; Coastal Kleberg; Coastal Nueces; Coastal San Patricio; Aransas Islands; Coastal Refugio; Coastal Calhoun; Kleberg Islands; Nueces Islands; Calhoun Islands\nSeverity: Severe\nDescription: * WHAT...Numerous flash floods likely with significant events\npossible.\n\n* WHERE...A portion of south Texas, including the following areas,\nAransas Islands, Bee, Calhoun Islands, Coastal Aransas, Coastal\nCalhoun, Coastal Kleberg, Coastal Nueces, Coastal Refugio, Coastal\nSan Patricio, Duval, Goliad, Inland Calhoun, Inland Kleberg,\nInland Nueces, Inland Refugio, Inland San Patricio, Jim Wells,\nKleberg Islands, La Salle, Live Oak, McMullen, Nueces Islands,\nVictoria and Webb.\n\n* WHEN...From 1 PM CDT this afternoon through Friday morning.\n\n* IMPACTS...Excessive runoff may result in flooding of rivers,\ncreeks, streams, and other low-lying and flood-prone locations.\nCreeks and streams may rise out of their banks. Flooding may occur\nin poor drainage and urban areas. Low-water crossings may be\nflooded.\n\n* ADDITIONAL DETAILS...\n- Abundant gulf moisture combined with a slow moving storm\nsystem will bring the potential for multiple rounds of\nshowers and thunderstorms, some with very heavy rainfall. The\nrain will begin this afternoon and continue through Friday\nmorning before ending. Given the hard dry soil from the lack\nof rainfall over the past several months, there is a\npotential for this heavy rain to quickly run off producing\nflash flooding.\n- http://www.weather.gov/safety/flood\nInstruction: You should monitor later forecasts and be prepared to take action\nshould Flash Flood Warnings be issued.\n\nThe Weather Prediction Center is forecasting a Moderate Risk for\nflash flooding. The probability for numerous significant flash\nfloods has increased. There is an increased danger to lives and\nproperty due to flash flooding.\n\n---\n\nEvent: Flood Watch\nArea: Wharton; Inland Jackson; Inland Matagorda; Inland Brazoria; Coastal Jackson; Coastal Matagorda; Coastal Brazoria; Matagorda Islands; Brazoria Islands\nSeverity: Severe\nDescription: * WHAT...Flooding caused by excessive rainfall is possible.\n\n* WHERE...Portions of south central and southeast Texas, including\nthe following areas, in south central Texas, Coastal Jackson and\nInland Jackson. In southeast Texas, Brazoria Islands, Coastal\nBrazoria, Coastal Matagorda, Inland Brazoria, Inland Matagorda,\nMatagorda Islands and Wharton.\n\n* WHEN...From Thursday morning through Friday afternoon.\n\n* IMPACTS...Excessive runoff may result in flooding of rivers,\ncreeks, streams, and other low-lying and flood-prone locations.\nFlooding may occur in poor drainage and urban areas.\n\n* ADDITIONAL DETAILS...\n- Showers and storms are expected early in the morning, though\nlulls in rainfall may occur throughout the daytime hours.\nHeavy rains and flash flooding potential are greatest during\nthe evening and overnight period through Friday morning.\nRainfall totals of 2.0-5.0 inches are forecasted across\nSoutheast Texas, though higher amounts of 5.0-9.0 inches are\npossible within the watch area.\n- http://www.weather.gov/safety/flood\nInstruction: You should monitor later forecasts and be alert for possible Flood\nWarnings. Those living in areas prone to flooding should be prepared\nto take action should flooding develop.\n\n---\n\nEvent: Flood Watch\nArea: Zapata; Jim Hogg; Brooks; Inland Kenedy; Coastal Kenedy; Kenedy Island\nSeverity: Severe\nDescription: * WHAT...Flash flooding caused by excessive rainfall is possible.\n\n* WHERE...A portion of Deep South Texas, including the following\nareas, Brooks, Coastal Kenedy, Inland Kenedy, Jim Hogg, Kenedy\nIsland and Zapata.\n\n* WHEN...From this evening through Friday morning.\n\n* IMPACTS...Excessive runoff may result in flooding of rivers,\ncreeks, streams, and other low-lying and flood-prone locations.\nFlooding may occur in poor drainage and urban areas. Storm drains\nand ditches may become clogged with debris.\n\n* ADDITIONAL DETAILS...\n- Abundant gulf moisture combined with a slow moving storm\nsystem will bring the potential for multiple rounds of\nshowers and thunderstorms, some with very heavy rainfall.\nThere is the potential for rapid runoff where excessive\nrainfall occurs over areas that have seen extensive drought\nconditions.\n- http://www.weather.gov/safety/flood\nInstruction: You should monitor later forecasts and be prepared to take action\nshould Flash Flood Warnings be issued.\n\n---\n\nEvent: Flood Warning\nArea: Beauregard, LA; Calcasieu, LA; Newton, TX; Orange, TX\nSeverity: Severe\nDescription: ...The Flood Warning continues for the following rivers in Texas...\nLouisiana...\n\nSabine River Near Deweyville\n\nAdditional information is available at www.weather.gov.\n\nThe next statement will be issued Wednesday evening at 745 PM CDT.\n\n* WHAT...Minor flooding is occurring and minor flooding is forecast.\n\n* WHERE...Sabine River near Deweyville.\n\n* WHEN...Until further notice.\n\n* IMPACTS...At 24.0 feet, Minor lowland flooding will occur.\n\n* ADDITIONAL DETAILS...\n- At 6:45 PM CDT Tuesday the stage was 24.3 feet.\n- Recent Activity...The maximum river stage in the 24 hours\nending at 6:45 PM CDT Tuesday was 24.4 feet.\n- Forecast...The river is expected to remain steady above flood\nstage at 24.3 feet.\n- Flood stage is 24.0 feet.\n- http://www.weather.gov/safety/flood\nInstruction: None\n"}],"isError":false}}

MCPサーバの動作

claude_desktop_config.json
TypeScript 版 {
"mcpServers": {
"weather": {
"command": "/Users/user.name/.asdf/shims/node",
"args": [
"/Users/user.name/path/to/weather/build/index.js"
]
}
}
}

MCP Client を作る
client のコード

Anthropic API - Messages
ユーザーからのリクエストに対して、MCP Client がどのようにツールを決定して利用するか、このあたり読めばイメージ付く

MCP Client を実行して MCP Server に問い合わせ
node build/index.js ../mcp-server-typescript/weather/build/index.js

呼び出し失敗してた
2025-03-26T23:37:15.987Z [weather] [info] Message from client: {"method":"prompts/list","params":{},"jsonrpc":"2.0","id":118}
2025-03-26T23:37:15.989Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":118,"error":{"code":-32601,"message":"Method not found"}}

mcp-get コマンドでMCPサーバを導入してみる
$ npx @michaellatman/mcp-get@latest
Available commands:
list List all available packages
install <package> Install a package
uninstall [package] Uninstall a package
installed List installed packages
update Update mcp-get to latest version
利用には注意

インストール済みの MCP Server
$ npx @michaellatman/mcp-get@latest installed
📦 Installed Packages
Found 1 installed packages
? Search and select a package:
❯ weather │ Installed package (not in package list) │ Unknown │ Unknown

MCP Server のインストール
$ npx @michaellatman/mcp-get@latest list
📦 Available Packages
Found 54 packages
? Search and select a package: filesystem
❯ @modelcontextprotocol/server-filesystem │ MCP server for filesystem access │ Anthropic, PBC (https://anthropic.com) │ MIT
mcp-server-aidd │ An MCP server that provides a comprehensive ... │ SkyDeck.ai® │ MIT
@niledatabase/nile-mcp-server │ MCP server for Nile Database - Manage and qu... │ Nile (https://www.thenile.dev/) │ MIT
@llmindset/mcp-hfspace │ MCP Server for using HuggingFace Spaces. Sea... │ llmindset.co.uk │ MIT
kubernetes-mcp-server │ Powerful and flexible Kubernetes MCP server ... │ Marc Nuri (https://www.marcnuri.com) │ Apache-2.0

ダウンロードと Claude Desktop の設定ファイルの更新までやってくれるっぽい
$ npx @michaellatman/mcp-get@latest list
📦 Available Packages
Found 54 packages
? Search and select a package: @modelcontextprotocol/server-filesystem
Package Details:
Name: @modelcontextprotocol/server-filesystem
Description: MCP server for filesystem access
Vendor: Anthropic, PBC (https://anthropic.com)
License: MIT
Runtime: node
Source: https://github.com/modelcontextprotocol/servers/blob/main/src/filesystem
Homepage: https://modelcontextprotocol.io
Status: Not installed
? What would you like to do? 📦 Install this package
Preparing to install @modelcontextprotocol/server-filesystem...
Updated Claude desktop configuration
? Would you like to help improve mcp-get by sharing anonymous installation analytics? (Y/n)

公式 MCP サーバのコードを読んでみる

Specification
(2025-03-26)
(2024-11-05)

LLM を使ってMCPサーバを作る
このスクラップは4ヶ月前にクローズされました