测试目的

了解MCP的机制原理

环境

代码运行在 VS Code 中
大模型:本地Ollama

代码

weather.py

from mcp.server.fastmcp import FastMCP

mcp=FastMCP("Weather")

@mcp.tool()
async def get_weather(location:str)->str:
    """Get the weather location."""
    return "It's always raining in California"

if __name__=="__main__":
    mcp.run(transport="streamable-http")

client.py

import langchain
langchain.debug = True
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_groq import ChatGroq
from langchain_ollama import ChatOllama

from dotenv import load_dotenv
load_dotenv()

import asyncio

async def main():
    client=MultiServerMCPClient(
        {
            "math":{
                "command":"python",
                "args":["mathserver.py"], ## Ensure correct absolute path
                "transport":"stdio",
            },
            "weather": {
                "url": "http://localhost:8000/mcp",  # Ensure server is running here
                "transport": "streamable_http",
            }

        }
    )

    import os
    os.environ["GROQ_API_KEY"]="gsk_**********"

    tools=await client.get_tools()
    # model=ChatGroq(model="qwen-qwq-32b")
    model = ChatOllama(model='qwen2.5:3b', base_url='http://localhost:11434')
    agent=create_react_agent(
        model,tools, debug=True
    )

    # math_response = await agent.ainvoke(
    #     {"messages": [{"role": "user", "content": "what's (3 + 5) x 12?"}]}
    # )

    # print("Math response:", math_response['messages'][-1].content)

    weather_response = await agent.ainvoke(
        {"messages": [{"role": "user", "content": "what is the weather in California?"}]}
    )
    print("Weather response:", weather_response['messages'][-1].content)

asyncio.run(main())

MultiServerMCPClient 是 LangChain 的一个适配器,用于管理多个外部工具或服务的调用。它支持不同的传输方式(stdiostreamable_http),允许灵活地与本地或远程服务交互。

ReAct(Reasoning + Acting)代理结合了语言模型的推理能力和工具的执行能力。流程如下:

  • 用户输入被送入语言模型(ChatOllama)。
  • 模型决定是直接回答还是调用工具(基于提示词和工具描述)。
  • 如果需要工具,模型生成 tool_calls(如 get_weather),并暂停推理。
  • 工具执行后,结果(ToolMessage)返回给模型。
  • 模型根据工具结果生成最终回答。

运行结果:

[chain/start] [chain:LangGraph] Entering Chain run with input:
{
  "messages": [
    {
      "role": "user",
      "content": "what is the weather in California?"
    }
  ]
}
[values] {'messages': [HumanMessage(content='what is the weather in California?', additional_kwargs={}, response_metadata={}, id='4f012859-5159-4468-9f32-127245db6ac3')]}
[chain/start] [chain:LangGraph > chain:agent] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:call_model] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:RunnableSequence] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:RunnableSequence > chain:Prompt] Entering Chain run with input:
[inputs]
[chain/end] [chain:LangGraph > chain:agent > chain:RunnableSequence > chain:Prompt] [0ms] Exiting Chain run with output:
[outputs]
[llm/start] [chain:LangGraph > chain:agent > chain:RunnableSequence > llm:ChatOllama] Entering LLM run with input:
{
  "prompts": [
    "Human: what is the weather in California?"
  ]
}
[llm/end] [chain:LangGraph > chain:agent > chain:RunnableSequence > llm:ChatOllama] [8.05s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "",
        "generation_info": {
          "model": "qwen2.5:3b",
          "created_at": "2025-07-07T09:36:11.0761893Z",
          "done": true,
          "done_reason": "stop",
          "total_duration": 8031389100,
          "load_duration": 3402588600,
          "prompt_eval_count": 273,
          "prompt_eval_duration": 3494796500,
          "eval_count": 20,
          "eval_duration": 1124573200,
          "model_name": "qwen2.5:3b"
        },
        "type": "ChatGeneration",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain",
            "schema",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "",
            "response_metadata": {
              "model": "qwen2.5:3b",
              "created_at": "2025-07-07T09:36:11.0761893Z",
              "done": true,
              "done_reason": "stop",
              "total_duration": 8031389100,
              "load_duration": 3402588600,
              "prompt_eval_count": 273,
              "prompt_eval_duration": 3494796500,
              "eval_count": 20,
              "eval_duration": 1124573200,
              "model_name": "qwen2.5:3b"
            },
            "type": "ai",
            "id": "run--4560e199-3a7f-4b4e-8885-cc1cdc8edb59-0",
            "tool_calls": [
              {
                "name": "get_weather",
                "args": {
                  "location": "California"
                },
                "id": "1d471665-c715-42a9-9919-fc10c5bfadd2",
                "type": "tool_call"
              }
            ],
            "usage_metadata": {
              "input_tokens": 273,
              "output_tokens": 20,
              "total_tokens": 293
            },
            "invalid_tool_calls": []
          }
        }
      }
    ]
  ],
  "llm_output": null,
  "run": null,
  "type": "LLMResult"
}
[chain/end] [chain:LangGraph > chain:agent > chain:RunnableSequence] [8.05s] Exiting Chain run with output:
[outputs]
[chain/end] [chain:LangGraph > chain:agent > chain:call_model] [8.05s] Exiting Chain run with output:
[outputs]
[chain/start] [chain:LangGraph > chain:agent > chain:should_continue] Entering Chain run with input:
[inputs]
[chain/end] [chain:LangGraph > chain:agent > chain:should_continue] [1ms] Exiting Chain run with output:
[outputs]
[chain/end] [chain:LangGraph > chain:agent] [8.06s] Exiting Chain run with output:
[outputs]
[updates] {'agent': {'messages': [AIMessage(content='', additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:11.0761893Z', 'done': True, 'done_reason': 'stop', 'total_duration': 8031389100, 'load_duration': 3402588600, 'prompt_eval_count': 273, 'prompt_eval_duration': 3494796500, 'eval_count': 20, 'eval_duration': 1124573200, 'model_name': 'qwen2.5:3b'}, id='run--4560e199-3a7f-4b4e-8885-cc1cdc8edb59-0', tool_calls=[{'name': 'get_weather', 'args': {'location': 'California'}, 'id': '1d471665-c715-42a9-9919-fc10c5bfadd2', 'type': 'tool_call'}], usage_metadata={'input_tokens': 273, 'output_tokens': 20, 'total_tokens': 293})]}}       
[values] {'messages': [HumanMessage(content='what is the weather in California?', additional_kwargs={}, response_metadata={}, id='4f012859-5159-4468-9f32-127245db6ac3'), AIMessage(content='', additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:11.0761893Z', 'done': True, 'done_reason': 'stop', 'total_duration': 8031389100, 'load_duration': 3402588600, 'prompt_eval_count': 273, 'prompt_eval_duration': 3494796500, 'eval_count': 20, 'eval_duration': 1124573200, 'model_name': 'qwen2.5:3b'}, id='run--4560e199-3a7f-4b4e-8885-cc1cdc8edb59-0', tool_calls=[{'name': 'get_weather', 'args': {'location': 'California'}, 'id': '1d471665-c715-42a9-9919-fc10c5bfadd2', 'type': 'tool_call'}], usage_metadata={'input_tokens': 273, 'output_tokens': 20, 'total_tokens': 293})]}        
[chain/start] [chain:LangGraph > chain:tools] Entering Chain run with input:
{
  "input": [
    {
      "name": "get_weather",
      "args": {
        "location": "California"
      },
      "id": "1d471665-c715-42a9-9919-fc10c5bfadd2",
      "type": "tool_call"
    }
  ]
}
[tool/start] [chain:LangGraph > chain:tools > tool:get_weather] Entering Tool run with input:
"{'location': 'California'}"
[tool/end] [chain:LangGraph > chain:tools > tool:get_weather] [795ms] Exiting Tool run with output:
"content="It's always raining in California" name='get_weather' tool_call_id='1d471665-c715-42a9-9919-fc10c5bfadd2'"
[chain/end] [chain:LangGraph > chain:tools] [795ms] Exiting Chain run with output:
[outputs]
[updates] {'tools': {'messages': [ToolMessage(content="It's always raining in California", name='get_weather', id='17ba2383-90e3-46f4-a465-3f6bbbbf1307', tool_call_id='1d471665-c715-42a9-9919-fc10c5bfadd2')]}}
[values] {'messages': [HumanMessage(content='what is the weather in California?', additional_kwargs={}, response_metadata={}, id='4f012859-5159-4468-9f32-127245db6ac3'), AIMessage(content='', additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:11.0761893Z', 'done': True, 'done_reason': 'stop', 'total_duration': 8031389100, 'load_duration': 3402588600, 'prompt_eval_count': 273, 'prompt_eval_duration': 3494796500, 'eval_count': 20, 'eval_duration': 1124573200, 'model_name': 'qwen2.5:3b'}, id='run--4560e199-3a7f-4b4e-8885-cc1cdc8edb59-0', tool_calls=[{'name': 'get_weather', 'args': {'location': 'California'}, 'id': '1d471665-c715-42a9-9919-fc10c5bfadd2', 'type': 'tool_call'}], usage_metadata={'input_tokens': 273, 'output_tokens': 20, 'total_tokens': 293}), ToolMessage(content="It's always raining in California", name='get_weather', id='17ba2383-90e3-46f4-a465-3f6bbbbf1307', tool_call_id='1d471665-c715-42a9-9919-fc10c5bfadd2')]}
[chain/start] [chain:LangGraph > chain:agent] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:call_model] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:RunnableSequence] Entering Chain run with input:
[inputs]
[chain/start] [chain:LangGraph > chain:agent > chain:RunnableSequence > chain:Prompt] Entering Chain run with input:
[inputs]
[chain/end] [chain:LangGraph > chain:agent > chain:RunnableSequence > chain:Prompt] [0ms] Exiting Chain run with output:
[outputs]
[llm/start] [chain:LangGraph > chain:agent > chain:RunnableSequence > llm:ChatOllama] Entering LLM run with input:
{
  "prompts": [
    "Human: what is the weather in California?\nAI: \nTool: It's always raining in California"
  ]
}
[llm/end] [chain:LangGraph > chain:agent > chain:RunnableSequence > llm:ChatOllama] [2.17s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "It's always raining in California. Is there a specific city or time frame you're interested in for weather updates? I can provide more detailed information if needed.",
        "generation_info": {
          "model": "qwen2.5:3b",
          "created_at": "2025-07-07T09:36:14.0550483Z",
          "done": true,
          "done_reason": "stop",
          "total_duration": 2161343000,
          "load_duration": 26617000,
          "prompt_eval_count": 316,
          "prompt_eval_duration": 348763500,
          "eval_count": 33,
          "eval_duration": 1776700200,
          "model_name": "qwen2.5:3b"
        },
        "type": "ChatGeneration",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain",
            "schema",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "It's always raining in California. Is there a specific city or time frame you're interested in for weather updates? I can provide more detailed information if needed.",
            "response_metadata": {
              "model": "qwen2.5:3b",
              "created_at": "2025-07-07T09:36:14.0550483Z",
              "done": true,
              "done_reason": "stop",
              "total_duration": 2161343000,
              "load_duration": 26617000,
              "prompt_eval_count": 316,
              "prompt_eval_duration": 348763500,
              "eval_count": 33,
              "eval_duration": 1776700200,
              "model_name": "qwen2.5:3b"
            },
            "type": "ai",
            "id": "run--747451bf-17f5-40e5-a03b-8ab1c9bcc83d-0",
            "usage_metadata": {
              "input_tokens": 316,
              "output_tokens": 33,
              "total_tokens": 349
            },
            "tool_calls": [],
            "invalid_tool_calls": []
          }
        }
      }
    ]
  ],
  "llm_output": null,
  "run": null,
  "type": "LLMResult"
}
[chain/end] [chain:LangGraph > chain:agent > chain:RunnableSequence] [2.18s] Exiting Chain run with output:
[outputs]
[chain/end] [chain:LangGraph > chain:agent > chain:call_model] [2.18s] Exiting Chain run with output:
[outputs]
[chain/start] [chain:LangGraph > chain:agent > chain:should_continue] Entering Chain run with input:
[inputs]
[chain/end] [chain:LangGraph > chain:agent > chain:should_continue] [1ms] Exiting Chain run with output:
{
  "output": "__end__"
}
[chain/end] [chain:LangGraph > chain:agent] [2.18s] Exiting Chain run with output:
[outputs]
[updates] {'agent': {'messages': [AIMessage(content="It's always raining in California. Is there a specific city or time frame you're interested in for weather updates? I can provide more detailed information if needed.", additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:14.0550483Z', 'done': True, 'done_reason': 'stop', 'total_duration': 2161343000, 'load_duration': 26617000, 'prompt_eval_count': 316, 'prompt_eval_duration': 348763500, 'eval_count': 33, 'eval_duration': 1776700200, 'model_name': 'qwen2.5:3b'}, id='run--747451bf-17f5-40e5-a03b-8ab1c9bcc83d-0', usage_metadata={'input_tokens': 316, 'output_tokens': 33, 'total_tokens': 349})]}}
[values] {'messages': [HumanMessage(content='what is the weather in California?', additional_kwargs={}, response_metadata={}, id='4f012859-5159-4468-9f32-127245db6ac3'), AIMessage(content='', additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:11.0761893Z', 'done': True, 'done_reason': 'stop', 'total_duration': 8031389100, 'load_duration': 3402588600, 'prompt_eval_count': 273, 'prompt_eval_duration': 3494796500, 'eval_count': 20, 'eval_duration': 1124573200, 'model_name': 'qwen2.5:3b'}, id='run--4560e199-3a7f-4b4e-8885-cc1cdc8edb59-0', tool_calls=[{'name': 'get_weather', 'args': {'location': 'California'}, 'id': '1d471665-c715-42a9-9919-fc10c5bfadd2', 'type': 'tool_call'}], usage_metadata={'input_tokens': 273, 'output_tokens': 20, 'total_tokens': 293}), ToolMessage(content="It's always raining in California", name='get_weather', id='17ba2383-90e3-46f4-a465-3f6bbbbf1307', tool_call_id='1d471665-c715-42a9-9919-fc10c5bfadd2'), AIMessage(content="It's always raining in California. Is there a specific city or time frame you're interested in for weather updates? I can provide more detailed information if needed.", additional_kwargs={}, response_metadata={'model': 'qwen2.5:3b', 'created_at': '2025-07-07T09:36:14.0550483Z', 'done': True, 'done_reason': 'stop', 'total_duration': 2161343000, 'load_duration': 26617000, 'prompt_eval_count': 316, 'prompt_eval_duration': 348763500, 'eval_count': 33, 'eval_duration': 1776700200, 'model_name': 'qwen2.5:3b'}, id='run--747451bf-17f5-40e5-a03b-8ab1c9bcc83d-0', usage_metadata={'input_tokens': 316, 'output_tokens': 33, 'total_tokens': 349})]}
[chain/end] [chain:LangGraph] [11.04s] Exiting Chain run with output:
[outputs]
Weather response: It's always raining in California. Is there a specific city or time frame you're interested in for weather updates? I can provide more detailed information if needed.

提示词

由于langchain封装了模型调用过程,看不到具体每次调用模型时传入的具体提示词

使用Wireshark进行抓包

第一次模型调用

{
    "model": "qwen2.5:3b",
    "stream": true,
    "options": {
    },
    "messages": [
        {
            "role": "user",
            "content": "what is the weather in California?"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "add",
                "description": "_summary_\n    Add to numbers\n    ",
                "parameters": {
                    "type": "object",
                    "required": [
                        "a",
                        "b"
                    ],
                    "properties": {
                        "a": {
                            "type": "integer"
                        },
                        "b": {
                            "type": "integer"
                        }
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "multiple",
                "description": "Multiply two numbers",
                "parameters": {
                    "type": "object",
                    "required": [
                        "a",
                        "b"
                    ],
                    "properties": {
                        "a": {
                            "type": "integer"
                        },
                        "b": {
                            "type": "integer"
                        }
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the weather location.",
                "parameters": {
                    "type": "object",
                    "required": [
                        "location"
                    ],
                    "properties": {
                        "location": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    ]
}

返回

{
    "model": "qwen2.5:3b",
    "created_at": "2025-07-08T00:43:57.6919442Z",
    "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
            {
                "function": {
                    "name": "get_weather",
                    "arguments": {
                        "location": "California"
                    }
                }
            }
        ]
    },
    "done": false
}
{
    "model": "qwen2.5:3b",
    "created_at": "2025-07-08T00:43:57.7934285Z",
    "message": {
        "role": "assistant",
        "content": ""
    },
    "done_reason": "stop",
    "done": true,
    "total_duration": 7427757000,
    "load_duration": 3209945000,
    "prompt_eval_count": 273,
    "prompt_eval_duration": 3245531500,
    "eval_count": 20,
    "eval_duration": 965036100
}

第二次模型调用

{
    "model": "qwen2.5:3b",
    "stream": true,
    "options": {
    },
    "messages": [
        {
            "role": "user",
            "content": "what is the weather in California?"
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "function": {
                        "name": "get_weather",
                        "arguments": {
                            "location": "California"
                        }
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": "It's always raining in California"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "add",
                "description": "_summary_\n    Add to numbers\n    ",
                "parameters": {
                    "type": "object",
                    "required": [
                        "a",
                        "b"
                    ],
                    "properties": {
                        "a": {
                            "type": "integer"
                        },
                        "b": {
                            "type": "integer"
                        }
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "multiple",
                "description": "Multiply two numbers",
                "parameters": {
                    "type": "object",
                    "required": [
                        "a",
                        "b"
                    ],
                    "properties": {
                        "a": {
                            "type": "integer"
                        },
                        "b": {
                            "type": "integer"
                        }
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the weather location.",
                "parameters": {
                    "type": "object",
                    "required": [
                        "location"
                    ],
                    "properties": {
                        "location": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    ]
}

反馈的内容

{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:58.8729945Z","message":{"role":"assistant","content":"The"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:58.9187418Z","message":{"role":"assistant","content":" weather"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:58.9640612Z","message":{"role":"assistant","content":" report"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.0099971Z","message":{"role":"assistant","content":" indicates"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.0611163Z","message":{"role":"assistant","content":" that"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.112749Z","message":{"role":"assistant","content":" it"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.1604754Z","message":{"role":"assistant","content":"'s"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.2083957Z","message":{"role":"assistant","content":" always"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.2563662Z","message":{"role":"assistant","content":" raining"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.307001Z","message":{"role":"assistant","content":" in"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.3580424Z","message":{"role":"assistant","content":" California"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.4213776Z","message":{"role":"assistant","content":"."},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.5045676Z","message":{"role":"assistant","content":" This"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.574449Z","message":{"role":"assistant","content":" might"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.6260499Z","message":{"role":"assistant","content":" be"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.6771508Z","message":{"role":"assistant","content":" due"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.7267104Z","message":{"role":"assistant","content":" to"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.7788253Z","message":{"role":"assistant","content":" the"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.8277021Z","message":{"role":"assistant","content":" frequent"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.8764309Z","message":{"role":"assistant","content":" occurrences"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.9246351Z","message":{"role":"assistant","content":" of"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:43:59.972042Z","message":{"role":"assistant","content":" precipitation"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.020537Z","message":{"role":"assistant","content":" events"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.0694191Z","message":{"role":"assistant","content":" such"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.1189758Z","message":{"role":"assistant","content":" as"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.1678503Z","message":{"role":"assistant","content":" fog"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.2182178Z","message":{"role":"assistant","content":","},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.2652884Z","message":{"role":"assistant","content":" dr"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.313335Z","message":{"role":"assistant","content":"izzle"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.3605889Z","message":{"role":"assistant","content":","},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.4087067Z","message":{"role":"assistant","content":" or"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.4591419Z","message":{"role":"assistant","content":" even"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.5140656Z","message":{"role":"assistant","content":" occasional"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.5644216Z","message":{"role":"assistant","content":" rain"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.6126341Z","message":{"role":"assistant","content":" throughout"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.6604703Z","message":{"role":"assistant","content":" the"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.7078443Z","message":{"role":"assistant","content":" year"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.7558939Z","message":{"role":"assistant","content":"."},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.803816Z","message":{"role":"assistant","content":" If"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.8518618Z","message":{"role":"assistant","content":" you"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.8995714Z","message":{"role":"assistant","content":" need"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:00.9508963Z","message":{"role":"assistant","content":" more"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.002031Z","message":{"role":"assistant","content":" specific"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.0509137Z","message":{"role":"assistant","content":" details"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.0999177Z","message":{"role":"assistant","content":" about"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.1479553Z","message":{"role":"assistant","content":" the"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.1966201Z","message":{"role":"assistant","content":" current"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.2449332Z","message":{"role":"assistant","content":" weather"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.2928803Z","message":{"role":"assistant","content":" conditions"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.3434815Z","message":{"role":"assistant","content":","},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.3924703Z","message":{"role":"assistant","content":" please"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.4413021Z","message":{"role":"assistant","content":" let"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.4930614Z","message":{"role":"assistant","content":" me"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.5428827Z","message":{"role":"assistant","content":" know"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.5949281Z","message":{"role":"assistant","content":"!"},"done":false}
{"model":"qwen2.5:3b","created_at":"2025-07-08T00:44:01.6448278Z","message":{"role":"assistant","content":""},"done_reason":"stop","done":true,"total_duration":3142359700,"load_duration":26605800,"prompt_eval_count":316,"prompt_eval_duration":337091800,"eval_count":56,"eval_duration":2772948700}

总结

所以,调用过程大概如下:

  1. client 将tool信息放到提示词中传递给模型
  2. 模型判断是否需要调用tool,是的话将调用的tool以及入参返回
  3. client根据上一步的返回内容调用tool
  4. client将调用tool的结果以及历史消息再发给模型
  5. 模型返回最终结果

Wireshark

在这里插入图片描述
开启捕获,运行MCP调用程序,然后停止捕获, 搜索过滤 tcp.port == 11434
在这里插入图片描述

拷贝消息内容
在这里插入图片描述

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐