FastMCPでnetwork情報のtoolを公開する3 / 4

Clientでの呼出し

serverをnetwork_info.pyへ保存した場合、次のscriptは同じprocess内のClientでtoolを発見し呼び出す。MCPの動作確認にLLMや外部AI APIは必要ない。

import asyncio
from fastmcp import Client
from network_info import mcp

async def main():
    async with Client(mcp) as client:
        tools = await client.list_tools()
        result = await client.call_tool("get_interface_state",
                         {"device_id":"BR1","interface":"GigabitEthernet1"})
        print([tool.name for tool in tools])
        print(result.data)

asyncio.run(main())

型から生成するschemaにはdevice_idの許可値と必要なinterfaceが現れる。実際の認可が利用者ごとに違う場合は、固定の許可値だけで終えず、認証した要求元とdeviceの閲覧範囲をserver側で評価する。

学習パスへ
参考資料