Token导航 LogoToken导航TokenDH.com
Minimal Client Weather Server Sample logo
AI代理未说明官方级别未说明来源级核验

Minimal Client Weather Server Sample

MCP Server

一个使用模型上下文协议(MCP)进行客户端-服务器交互的简单天气查询工具,用于演示目的。

工具数

1

提示词数

0

GitHub Stars

0

资源数

0
位置天气PythonClaudeClaude

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

takahirom

提供方

takahirom

最后核验

2026/5/17 20:23

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

MCP(模型上下文协议)客户端-服务器交互示例

一个简单的天气工具,演示使用模型上下文协议(MCP)的服务器-客户端交互。仅用于演示目的。

用户:“东京的天气怎么样?”\ 回复:“东京的天气晴朗。”

图1:初始化和工具发现

sequenceDiagram
    autonumber

    participant User
    participant ClientApp as ClientApp (Host)
on Local PC
    participant LLM as LLM (e.g., Claude)
Remote Service
    participant MCPClient as MCPClient (Internal Component)
in ClientApp on Local PC
    participant MCPServer as MCPServer (e.g., Tool Server)
on Local PC

    Note over User, MCPServer: User starts ClientApp, initiating connection to MCPServer

    ClientApp->>+MCPClient: Instruct preparation to connect to a specific MCPServer
    Note right of ClientApp: Host manages MCPClient instances per server

    MCPClient->>+MCPServer: 1. initialize (Request)
[protocol_version, client_capabilities]
    Note over MCPClient, MCPServer: Start connection establishment and capability exchange (JSON-RPC)
    MCPServer-->>-MCPClient: 2. initialize (Response)
[selected_protocol_version, server_capabilities (tools, resources, etc.)]
    Note over MCPClient, MCPServer: Server notifies its available capabilities

    MCPClient->>MCPServer: 3. notifications/initialized (Initialization Complete Notification)
    Note over MCPClient, MCPServer: Handshake complete, normal communication possible

    MCPClient-->>-ClientApp: Initialization Success & Server Capabilities notified (implicitly via successful initialize await)
    Note right of ClientApp: Host (ClientApp) now knows the server is ready

    ClientApp->>MCPClient: 4. Instruct to get the list of tools provided by the server (session.list_tools())
    MCPClient->>+MCPServer: 5. tools/list (Request)
    Note over MCPClient, MCPServer: Request the list of tools defined on the server
    MCPServer-->>-MCPClient: 6. tools/list (Response)
[{"name": "get_weather", "description": "...", "inputSchema": {...}}]
    Note over MCPClient, MCPServer: Example: Returns the definition of the 'get_weather' tool

    MCPClient-->>ClientApp: 7. Notify Tool List (containing 'get_weather', etc.) via tools_response object
    Note right of ClientApp: Host stores/processes the retrieved tool information (format_tools_for_llm)

    ClientApp->>LLM: 8. Send available tool information to LLM
(e.g., via system prompt using formatted tools_prompt)
    Note over ClientApp, LLM: Host informs LLM that 'get_weather' is available
LLM can now decide to use the tool based on this info

图2:工具执行流程

sequenceDiagram
    autonumber

    participant User
    participant ClientApp as ClientApp (Host)
on Local PC
    participant LLM as LLM (e.g., Claude)
Remote Service
    participant MCPClient as MCPClient (Internal Component)
in ClientApp on Local PC
    participant MCPServer as MCPServer (e.g., Tool Server)
on Local PC

    User->>ClientApp: 1. "What's the weather in Tokyo?"
    Note right of User: User asks a question that might require a tool

    ClientApp->>LLM: 2. Forward user's question with context
[System Prompt (with tool info) + User Question]
    Note over ClientApp, LLM: Host sends the question and formatted tool info ('get_weather') to the LLM

    LLM->>ClientApp: 3. LLM responds, requesting tool usage
Response: `{"tool_name": "get_weather", "arguments": {"location": "Tokyo"}}`
    Note over ClientApp, LLM: Based on provided tool info, LLM decides to use the weather tool
and generates the required JSON structure with arguments

    ClientApp->>User: (Optional) 4. Confirm tool execution
"Execute 'get_weather' on Tool Server?"
    User->>ClientApp: (Optional) 5. "Yes"
    Note over ClientApp, User: For security/transparency, host might seek user permission (Not implemented in the sample code)

    ClientApp->>+MCPClient: 6. Instruct execution of 'get_weather' tool (session.call_tool())
Arguments: {"location": "Tokyo"}
    Note right of ClientApp: Host instructs the MCP server via MCPClient

    MCPClient->>+MCPServer: 7. tools/call (Request)
[name: "get_weather", arguments: {"location": "Tokyo"}]
    Note over MCPClient, MCPServer: Invoke server function with specified tool name and arguments (JSON-RPC)

    MCPServer->>MCPServer: Internal processing (executes get_weather function)
    Note over MCPServer: Logs execution, returns result (e.g., "Sunny")
    MCPServer-->>-MCPClient: 8. tools/call (Response)
[result: {content: [{type: "text", text: "Sunny"}]}]
    Note over MCPClient, MCPServer: Server returns the processing result (weather info) in structured format

    MCPClient-->>-ClientApp: 9. Notify Tool Execution Result
Result object contains: "Sunny" (extracted from tool_result_obj.content[0].text)

    ClientApp->>LLM: 10. Send tool execution result back to LLM
New User Message: "Tool 'get_weather' returned: 'Sunny'. Answer the original question based on this."
    Note over ClientApp, LLM: Host feeds back the information obtained from the server to the LLM

    LLM->>ClientApp: 11. Generate final response (as text)
"The weather in Tokyo is Sunny."

    ClientApp->>User: 12. Display the final response generated by LLM
    Note right of ClientApp: Present the final answer to the user

目录标签

目录标签

位置天气PythonClaude天气查询本地部署客户端-服务器交互模型上下文协议演示工具

支持客户端

Claude

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

session

工具数量(toolCount,工具数)

1

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明session部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP