mcp独立模块

This commit is contained in:
SengokuCola
2026-03-29 14:39:14 +08:00
parent 5876f246c0
commit f32edfa732
6 changed files with 19 additions and 26 deletions

View File

@@ -1,13 +0,0 @@
{
"mcpServers": {
"tavily": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"https://mcp.tavily.com/mcp/?tavilyApiKey=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"
],
"env": {}
}
}
}

View File

@@ -1,12 +1,12 @@
"""
MaiSaka - MCP (Model Context Protocol) 客户端包
MCP (Model Context Protocol) 客户端包
提供 MCPManager 用于管理 MCP 服务器连接发现工具调用工具
用法:
from .manager import MCPManager
manager = await MCPManager.from_config("mcp_config.json")
manager = await MCPManager.from_config("config/mcp_config.json")
if manager:
tools = manager.get_openai_tools() # 获取 OpenAI 格式工具列表
result = await manager.call_tool(name, args) # 调用工具

View File

@@ -1,6 +1,6 @@
"""
MaiSaka - MCP 配置加载与验证
mcp_config.json 读取 MCP 服务器定义解析为结构化配置对象
MCP 配置加载与验证
config/mcp_config.json 读取 MCP 服务器定义解析为结构化配置对象
配置格式示例:
{
@@ -21,12 +21,16 @@ MaiSaka - MCP 配置加载与验证
- url: SSE 传输连接远程服务器
"""
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
import json
import os
from dataclasses import dataclass, field
from typing import Optional
from ..console import console
from src.maisaka.console import console
DEFAULT_MCP_CONFIG_PATH = Path(__file__).resolve().parents[2] / "config" / "mcp_config.json"
@dataclass
@@ -54,7 +58,7 @@ class MCPServerConfig:
return "unknown"
def load_mcp_config(config_path: str = "mcp_config.json") -> list[MCPServerConfig]:
def load_mcp_config(config_path: str = str(DEFAULT_MCP_CONFIG_PATH)) -> list[MCPServerConfig]:
"""
从配置文件加载 MCP 服务器列表
@@ -76,7 +80,7 @@ def load_mcp_config(config_path: str = "mcp_config.json") -> list[MCPServerConfi
mcp_servers = data.get("mcpServers", {})
if not isinstance(mcp_servers, dict):
console.print("[warning]⚠️ mcp_config.json 中 mcpServers 格式无效[/warning]")
console.print("[warning]⚠️ MCP 配置中的 mcpServers 格式无效[/warning]")
return []
configs: list[MCPServerConfig] = []

View File

@@ -6,7 +6,8 @@ MaiSaka - 单个 MCP 服务器连接管理
from contextlib import AsyncExitStack
from typing import Any, Optional
from ..console import console
from src.maisaka.console import console
from .config import MCPServerConfig
# ──────────────────── MCP SDK 可选导入 ────────────────────

View File

@@ -5,8 +5,9 @@ MaiSaka - MCP 管理器
from typing import Optional
from ..console import console
from .config import MCPServerConfig, load_mcp_config
from src.maisaka.console import console
from .config import DEFAULT_MCP_CONFIG_PATH, MCPServerConfig, load_mcp_config
from .connection import MCPConnection, MCP_AVAILABLE
# 内置工具名称集合 —— MCP 工具不允许与这些名称冲突
@@ -43,7 +44,7 @@ class MCPManager:
@classmethod
async def from_config(
cls,
config_path: str = "mcp_config.json",
config_path: str = str(DEFAULT_MCP_CONFIG_PATH),
) -> Optional["MCPManager"]:
"""
从配置文件创建并初始化 MCPManager