Coze-MCP 集成工具 将 Coze 智能体集成到 MCP (Model Context Protocol) 平台的工具,实现流式对话功能,使 Coze AI 能够作为 MCP 客户端的工具资源。 功能特点 ✅ 集成 Coze 智能体到 MCP 平台 ✅ 支持流式对话,实时返回回复片段 ✅ 完整的日志记录和错误处理 ✅ 支持环境变量配置,便于生产环境部署 ✅ 可扩展的工具函数设计 安装要求 依赖环境 Python 3.7+ Coze Python SDK MCP SDK 安装步骤 安装 Coze SDK: bash pip install cozepy 安装 MCP SDK: bash pip install mcp 环境变量配置 在运行前,需要设置以下环境变量: bash
Coze API 访问令牌
export COZE_API_TOKEN=你的Coze_API令牌
Coze 机器人ID
export COZE_BOT_ID=你的Coze机器人ID
用户ID (可选)
export COZE_USER_ID=用户标识 快速开始 启动 MCP 服务器 bash python src\mcp-connect-coze\connect_coze.py 使用 MCP 客户端调用工具 python from mcp.client import MCPClient
初始化 MCP 客户端
client = MCPClient(transport='stdio')
调用 chat_coze 工具
question = "请介绍一下人工智能在企业管理中的应用" response = client.call("chat_coze", question=question)
处理流式响应
for chunk in response: if chunk["status"] == "streaming": print(chunk["content"], end="", flush=True) elif chunk["status"] == "completed": print(f"\n对话完成,使用令牌: {chunk['token_usage']}") 代码结构说明 核心组件 python from mcp.server.fastmcp import FastMCP from cozepy import Coze, TokenAuth, Message, ChatEventType FastMCP: MCP 服务器核心类 Coze: Coze Python SDK 客户端 TokenAuth: Coze 认证类 Message: Coze 消息类 ChatEventType: Coze 聊天事件类型 主要函数 python @mcp.tool() def chat_coze(question: str) -> Generator[Dict[str, Any], None, None]: 这是核心工具函数,接收用户问题并返回流式响应。使用生成器 (Generator) 实现实时响应功能。 工具函数说明 chat_coze 函数 功能: 与 Coze AI 机器人对话并流式返回结果 参数: question (str): 用户提问的问题文本 返回: Generator[Dict[str, Any], None, None]: 流式返回的消息片段,每个片段包含以下字段: status: 状态 (started/streaming/completed/error) question: 原始问题 chunk_type: 片段类型 (system/content/metadata/error) content: 消息内容 (仅当 chunk_type 为 content 时存在) token_usage: 令牌使用量 (仅当对话完成时存在) is_final: 是否为最终片段 error: 错误信息 (仅当发生错误时存在) 日志说明 本工具使用 Python 标准 logging 模块,日志配置如下: python
配置日志
logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
Metadata
Release files for mcp-connect 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mcp_connect-0.1.6.tar.gz | 5.0 kB | Without provenance | Details for mcp_connect-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mcp_connect-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for mcp_connect-0.1.6-py3-none-any.whl |
Total release size: 10.2 kB
Release files / mcp_connect-0.1.6.tar.gz
| Download URL | mcp_connect-0.1.6.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d84209ab93ffc7a4fa7a0d9135ef83bec1789d32c7806aa4025f15764a2b124
|
|
BLAKE2b-256 checksum How to use checksums |
593d556eaae830fbe3f4fb18275f28cff1b3ed2b85833e894e54986573c45cb1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Release files / mcp_connect-0.1.6-py3-none-any.whl
| Download URL | mcp_connect-0.1.6-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
82ff3d6d664058e6ea877b57056389c9f2cd5e952c83d3b0fca80cef0c35fb75
|
|
BLAKE2b-256 checksum How to use checksums |
2fe07fab9dbac6c992939ae386a915ad4aefaa972e9f9970e808cd38a6515a8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Security for mcp-connect 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.