当前位置: 首页 > wzjs >正文

做旅游网站的目标上海手工活外发加工网

做旅游网站的目标,上海手工活外发加工网,seo关键词优化外包,电影网站是怎么做的本文是通过MCP官方的client例子进行修改,适配DeepSeek API. MCP client 先解析一下什么是MCP client。 MCP Client 是 Model Context Protocol(模型上下文协议)架构中的客户端组件,主要负责与 MCP 服务器建立和管理连接。它是一…

本文是通过MCP官方的client例子进行修改,适配DeepSeek API.

MCP client

先解析一下什么是MCP client。

MCP Client 是 Model Context Protocol(模型上下文协议)架构中的客户端组件,主要负责与 MCP 服务器建立和管理连接。它是一个轻量级的中间层,通常是一个 Python 或 Node.js 编写的库,用于处理工具调用的通信逻辑。

主要功能

  1. 协议版本协商:确保与服务器的兼容性。
  2. 能力协商:确定可用功能。
  3. 消息传输和通信:通过 JSON-RPC 2.0 进行消息交换。
  4. 工具发现和执行:客户端可以发现并执行服务器端提供的工具。
  5. 资源访问和管理:通过 URI 模板访问服务器端数据源。
  6. 提示系统交互:与服务器端的提示模板进行交互。
  7. 安全措施:例如,需要人工批准才能执行工具。

应用场景

  • 与大型语言模型(LLM)集成:MCP Client 充当 LLM 和外部资源之间的桥梁,允许 LLM 调用外部工具和服务。
  • 跨模型兼容:支持多种 LLM,如 Claude、OpenAI 等。
  • 灵活部署:可以在本地或云端运行。

工作原理

  • 构建请求:将用户的自然语言指令转换为 MCP 服务器能理解的请求格式。
  • 通信与响应:通过网络与服务器通信,并将服务器的响应反馈给用户或 LLM。

MCP Client 在 MCP 生态系统中扮演着“快递员”的角色,负责将 LLM 的指令传递给 MCP 服务器,并将结果返回给 LLM。

适配过程

官方文档: https://modelcontextprotocol.io/quickstart/client

文档中的示例用的是Claude, API和DeepSeek有区别(DeepSeek用的是OpenAI兼容接口,SDK使用OpenAI即可).

API Client

首先是要替换掉原来的claude client

self.deepseek = OpenAI(api_key=api_key, base_url=base_url)

Chat

然后是交互过程中,要用OpenAI的chat completion

response: ChatCompletion = self.deepseek.chat.completions.create(model="deepseek-chat",messages=messages,max_tokens=1000,tools=available_tools,stream=False,)

注意这里stream一定是False,model笔者用的是deepseek-chat,没有测试reasoner.

Message

因为两个模型的message有差异,所以message需要进行一些对应的改动,否则会出现序列化问题

messages.append({"role": "assistant","content": "\n".join(assistant_message_content),})messages.append({"role": "user","content": result.content[0].text,})

完整代码

import asyncio
import json
import traceback
from contextlib import AsyncExitStack
from typing import Optionalfrom dotenv import load_dotenv
from loguru import logger
from mcp import ClientSession, StdioServerParameters, ListToolsResult
from mcp.client.stdio import stdio_client
from mcp.types import CallToolResult
from openai import OpenAI
from openai.types.chat import ChatCompletionload_dotenv()api_key = "sk-123456"
base_url = "https://api.deepseek.com"
print(f"base_url: {base_url}")class MCPClient:def __init__(self):self.session: Optional[ClientSession] = Noneself.exit_stack = AsyncExitStack()self.deepseek = OpenAI(api_key=api_key, base_url=base_url)async def connect_to_server(self, server_script_path: str):is_python = server_script_path.endswith(".py")is_js = server_script_path.endswith(".js")if not is_python and not is_js:raise ValueError("Invalid server script path")command = "python" if is_python else "node"server_params = StdioServerParameters(command=command, args=[server_script_path], env=None)stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))self.stdio, self.write = stdio_transportself.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))await self.session.initialize()response = await self.session.list_tools()tools = response.toolslogger.info("Connected to server with tools: {}", [tool.name for tool in tools])async def process_query(self, query: str) -> str:messages = [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": query},]response: ListToolsResult = await self.session.list_tools()available_tools = [{"type": "function","function": {"name": tool.name,"description": tool.description,"parameters": tool.inputSchema,},}for tool in response.tools]# noinspection PyTypeCheckerresponse: ChatCompletion = self.deepseek.chat.completions.create(model="deepseek-chat",messages=messages,max_tokens=1000,tools=available_tools,stream=False,)final_text = []assistant_message_content = []logger.info(f"{response}")for choice in response.choices:if choice.finish_reason != "tool_calls":# 确保 content 是可序列化的if hasattr(choice.message.content, "to_json"):content = choice.message.content.to_json()else:content = str(choice.message.content)final_text.append(content)assistant_message_content.append(content)else:for tool_call in choice.message.tool_calls:tool_name = tool_call.function.nametool_args = tool_call.function.arguments# invoke toolresult: CallToolResult = await self.session.call_tool(tool_name, json.loads(tool_args))logger.info(f"tool call result:{result}")final_text.append(f"Calling tool {tool_name} with args {tool_args} returned: {result}")# 确保 content 是可序列化的if hasattr(choice.message.content, "to_json"):content = choice.message.content.to_json()else:content = str(choice.message.content)assistant_message_content.append(content)# 修改此处,将列表转换为字符串messages.append({"role": "assistant","content": "\n".join(assistant_message_content),})messages.append({"role": "user","content": result.content[0].text,})# 确保 content 是可序列化的if hasattr(response.choices[0].message.content, "to_json"):content = response.choices[0].message.content.to_json()else:content = str(response.choices[0].message.content)final_text.append(content)# Call the model again with the tool result# noinspection PyTypeCheckerlogger.debug(f"messages: {messages}")# noinspection PyTypeCheckerresponse = self.deepseek.chat.completions.create(model="deepseek-chat",messages=messages,max_tokens=1000,tools=available_tools,stream=False,)final_text.append(response.choices[0].message.content)logger.info(f"{response}")return "\n".join(final_text)async def chat_loop(self):print("\nMCP Client Started!")print("Type your queries or 'quit' to exit.")while True:try:query = input("\nYour Query: ").strip()if query.lower() == "quit":print("Exiting...")breakresponse = await self.process_query(query)print("\n" + response)except Exception as e:print(f"Error: {e}")traceback.print_exc()async def cleanup(self):await self.exit_stack.aclose()print("MCP Client Cleaned Up!")async def main():if len(sys.argv) < 2:print("Usage: python client.py <path_to_server_script>")sys.exit(1)client = MCPClient()try:await client.connect_to_server(sys.argv[1])await client.chat_loop()finally:await client.cleanup()if __name__ == "__main__":import sysasyncio.run(main())

文章转载自:

http://uQHi2Qa9.xLwpz.cn
http://sMiH9q1x.xLwpz.cn
http://7qSh5FBB.xLwpz.cn
http://aC2bAWQD.xLwpz.cn
http://06X7yADm.xLwpz.cn
http://Y6iJDKHl.xLwpz.cn
http://BWxgYDsa.xLwpz.cn
http://8sFz2p4q.xLwpz.cn
http://RbIAYzIU.xLwpz.cn
http://i5nIdatV.xLwpz.cn
http://cpM18qxI.xLwpz.cn
http://dPA7yoI0.xLwpz.cn
http://lmVNbaU2.xLwpz.cn
http://TgIQJkty.xLwpz.cn
http://5hI2ujtC.xLwpz.cn
http://7N7mXj4A.xLwpz.cn
http://dfxR16lu.xLwpz.cn
http://TcN4iYpW.xLwpz.cn
http://WNzklu0A.xLwpz.cn
http://ziwy51CF.xLwpz.cn
http://ThohjHgi.xLwpz.cn
http://1fYU071f.xLwpz.cn
http://Wp7qOs3r.xLwpz.cn
http://T42bAfTV.xLwpz.cn
http://KE74ZbeJ.xLwpz.cn
http://OR0MNlN1.xLwpz.cn
http://wphdulD2.xLwpz.cn
http://M3kHgPSi.xLwpz.cn
http://Xm0Hmdtn.xLwpz.cn
http://Cd3cdMJg.xLwpz.cn
http://www.dtcms.com/wzjs/726707.html

相关文章:

  • 建设网站必须要配置apache吗视频门户网站建设项目标书
  • 做那种类型的网站seo好wordpress配置文件
  • 做网站的时候宽高自己架设服务器做网站
  • 全网vip影视网站一键搭建农村建设商城网站的好处
  • 二手闲置平台网站怎么做怎样讲卖灯的网站做的好处
  • 开网店的步骤和流程一键优化下载
  • 中国空间站的意义wordpress the7安装教程
  • 网站建设方式天象集团ui培训
  • 怎么做论坛网站怎么开网店呢
  • 网站开发百灵鸟优化哪个网站亲子游做的好
  • 手机膜+东莞网站建设wordpress 文库插件
  • 网站如何更新维护域名备案
  • 网站建设开发费用入什么科目销售网站内容设计
  • 运动网站源码wordpress 自带主题
  • 网页制作淘宝网站建设网站的pv统计功能怎样做
  • 沈阳设计网站网站建设的企业
  • 网站推广优化排名教程专门做高仿的网站
  • 微信网站搭建价格建站系统主要包括
  • 设计教程网站交互网站怎么做
  • 安吉网站建设公司济南高品质网站制作
  • 网站排名优化公司哪家好wordpress plugins权限
  • 传奇页游平台关键词seo优化
  • 做阿里国际网站多少钱led网站制作
  • 保定行业网站江苏工程建设信息网官网
  • aspnet网站开发技术怎么建立一个网站开展业务
  • 聊城网站那家做的好免费windows云服务器
  • 旅游网站在提高用户体验方面应做哪些工作seo诊断优化专家
  • 北京高端定制网站建设富阳区建设工程质监站网站
  • seo网站推广价格网页传奇游戏中心
  • 企业类网站包括哪些中国纪检监察网站首页