WeKnora/mcp-server/main.py

179 lines
5.5 KiB
Python
Raw Normal View History

2025-09-08 05:14:35 +00:00
#!/usr/bin/env python3
"""
WeKnora MCP Server 主入口点
这个文件提供了一个统一的入口点来启动 WeKnora MCP 服务器。
可以通过多种方式运行:
1. python main.py
2. python -m weknora_mcp_server
3. weknora-mcp-server (安装后)
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
注意:在 stdio 传输下,stdout 是 JSON-RPC 通道,所有诊断/提示信息必须写入
stderr,否则会破坏 MCP 协议流导致客户端判定"启动失败"。本文件所有 print
均通过 stderr 输出。
2025-09-08 05:14:35 +00:00
"""
import argparse
import asyncio
2025-09-08 05:14:35 +00:00
import os
import sys
from pathlib import Path
2025-09-08 05:14:35 +00:00
def setup_environment():
"""设置环境和路径"""
# 确保当前目录在 Python 路径中
current_dir = Path(__file__).parent.absolute()
if str(current_dir) not in sys.path:
sys.path.insert(0, str(current_dir))
2025-09-08 05:14:35 +00:00
def check_dependencies():
"""检查依赖是否已安装"""
try:
import mcp
import requests
2025-09-08 05:14:35 +00:00
return True
except ImportError as e:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print(f"缺少依赖: {e}", file=sys.stderr)
print("请运行: pip install -r requirements.txt", file=sys.stderr)
2025-09-08 05:14:35 +00:00
return False
2025-09-08 05:14:35 +00:00
def check_environment_variables():
"""检查环境变量配置"""
base_url = os.getenv("WEKNORA_BASE_URL")
api_key = os.getenv("WEKNORA_API_KEY")
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("=== WeKnora MCP Server 环境检查 ===", file=sys.stderr)
print(f"Base URL: {base_url or 'http://localhost:8080/api/v1 (默认)'}", file=sys.stderr)
print(f"API Key: {'已设置' if api_key else '未设置 (警告)'}", file=sys.stderr)
2025-09-08 05:14:35 +00:00
if not base_url:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("提示: 可以设置 WEKNORA_BASE_URL 环境变量", file=sys.stderr)
2025-09-08 05:14:35 +00:00
if not api_key:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("警告: 建议设置 WEKNORA_API_KEY 环境变量", file=sys.stderr)
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("=" * 40, file=sys.stderr)
2025-09-08 05:14:35 +00:00
return True
2025-09-08 05:14:35 +00:00
def parse_arguments():
"""解析命令行参数"""
parser = argparse.ArgumentParser(
description="WeKnora MCP Server - Model Context Protocol server for WeKnora API",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
python main.py # 使用默认配置启动
python main.py --check-only # 仅检查环境,不启动服务器
python main.py --verbose # 启用详细日志
环境变量:
WEKNORA_BASE_URL WeKnora API 基础 URL (默认: http://localhost:8080/api/v1)
WEKNORA_API_KEY WeKnora API 密钥
MCP_SERVER_AUTH_TOKEN SSE/HTTP 传输必填,客户端通过 Authorization: Bearer 传递
""",
2025-09-08 05:14:35 +00:00
)
2025-09-08 05:14:35 +00:00
parser.add_argument(
"--check-only", action="store_true", help="仅检查环境配置,不启动服务器"
2025-09-08 05:14:35 +00:00
)
parser.add_argument("--verbose", "-v", action="store_true", help="启用详细日志输出")
2025-09-08 05:14:35 +00:00
parser.add_argument(
fix(mcp-server): migrate to MCPServer high-level API (mcp 2.x) [v1.1.0] MCP Python SDK 2.0 removed the low-level Server decorator API (@app.list_tools() / @app.call_tool() / app.get_capabilities()), so the server crashed at startup with: AttributeError: 'Server' object has no attribute 'list_tools' The released PyPI package weknora-mcp==1.0.1 declared 'mcp>=1.0.0' with no upper bound, so uvx resolved to the latest 2.0.0 and the plugin's weknora MCP server could no longer connect. Fix: rewrite the server on top of the mcp 2.x high-level MCPServer API (formerly FastMCP), and cap the dependency at 'mcp>=2,<3'. - weknora_mcp_server.py: replace @app.list_tools()/@app.call_tool() with 28 @mcp.tool() function-signature tools (input schema auto-generated from type hints, descriptions from docstrings, plain return values serialized by the framework). Transports use run_stdio_async() / sse_app() / streamable_http_app(). WeKnoraClient business logic is unchanged. - deps: mcp>=2,<3 in pyproject.toml / requirements.txt / setup.py; commit uv.lock pinning mcp 2.0.0. - version bump to 1.1.0 (pyproject, setup, __init__, main --version, server_version). - check_imports.py: validate MCPServer / ToolError / mcp_types imports. - ci(.github/workflows/mcp-server.yml): publish on merge to main + manual dispatch, not only on tags; add PyPI version-dedup check so re-runs on an unchanged version stay green (absorbed from PR #2399). Verified: stdio initialize + tools/list returns all 28 tools; no list_tools AttributeError; tool dispatch + error wrapping confirmed. Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-30 06:44:25 +00:00
"--version", action="version", version="WeKnora MCP Server 1.1.0"
2025-09-08 05:14:35 +00:00
)
parser.add_argument(
"--transport",
choices=["stdio", "sse", "http"],
default=os.getenv("MCP_TRANSPORT", "stdio"),
help="Transport type: stdio (default), sse, or http",
)
parser.add_argument(
"--host",
default=os.getenv("MCP_HOST", "127.0.0.1"),
help="Bind host for network transports (default: 127.0.0.1)",
)
parser.add_argument(
"--port",
type=int,
default=int(os.getenv("MCP_PORT", "8000")),
help="Bind port for network transports (default: 8000)",
)
2025-09-08 05:14:35 +00:00
return parser.parse_args()
2025-09-08 05:14:35 +00:00
async def main():
"""主函数"""
args = parse_arguments()
2025-09-08 05:14:35 +00:00
# 设置环境
setup_environment()
2025-09-08 05:14:35 +00:00
# 检查依赖
if not check_dependencies():
sys.exit(1)
2025-09-08 05:14:35 +00:00
# 检查环境变量
check_environment_variables()
2025-09-08 05:14:35 +00:00
# 如果只是检查环境,则退出
if args.check_only:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("环境检查完成。", file=sys.stderr)
2025-09-08 05:14:35 +00:00
return
2025-09-08 05:14:35 +00:00
# 设置日志级别
if args.verbose:
import logging
2025-09-08 05:14:35 +00:00
logging.basicConfig(level=logging.DEBUG)
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("已启用详细日志模式", file=sys.stderr)
2025-09-08 05:14:35 +00:00
try:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print(f"正在启动 WeKnora MCP Server (transport={args.transport})...", file=sys.stderr)
from weknora_mcp_server import run_stdio, run_sse, run_http
# Select transport mode based on CLI argument or MCP_TRANSPORT env var
# - stdio: Default, used by VS Code Copilot for local integration
# - sse: Server-Sent Events over HTTP, suitable for cloud/remote deployments
# - http: Streamable HTTP sessions (MCP 2025-03-26 spec), compatible with REST clients
if args.transport == "stdio":
# Stdio mode: communication via stdin/stdout pipes (typical for CLI integrations)
await run_stdio()
elif args.transport == "sse":
# SSE mode: HTTP server with Server-Sent Events for bidirectional streaming
await run_sse(args.host, args.port)
elif args.transport == "http":
# HTTP mode: HTTP REST server with request/response model
await run_http(args.host, args.port)
2025-09-08 05:14:35 +00:00
except ImportError as e:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print(f"导入错误: {e}", file=sys.stderr)
print("请确保所有文件都在正确的位置", file=sys.stderr)
2025-09-08 05:14:35 +00:00
sys.exit(1)
except KeyboardInterrupt:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print("\n服务器已停止", file=sys.stderr)
2025-09-08 05:14:35 +00:00
except Exception as e:
fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) * fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash Entry scripts (run_server.py, main.py, run.py) printed diagnostics to stdout via bare print(). Under the stdio transport (the default and the path used by all MCP clients per MCP_CONFIG.md: `uv run run_server.py`), stdout is the JSON-RPC channel, so the plain-text lines emitted before the initialize handshake corrupted the protocol stream. Strict MCP clients (Claude Desktop / Cursor / ZCode / VS Code Copilot) treated this as a fatal protocol error and reported the server as failed-to-start. Redirect all diagnostic print() to sys.stderr (per MCP spec, only JSON-RPC may use stdout). weknora_mcp_server.py already routes through logging (stderr) and is unchanged. Verified against a live backend (initialize -> tools/list (28 tools) -> tools/call list_knowledge_bases); stdout first byte is now '{' with all diagnostics on stderr. * chore(mcp-server): publish package name as weknora-mcp Rename the PyPI distribution name from weknora-mcp-server to weknora-mcp (tools used: pyproject [project].name + setup.py name). The installed console-script commands (weknora-mcp-server, weknora-server) and the Python module (weknora_mcp_server) are unchanged for backward compatibility. * fix(mcp-server): include upload_paths in wheel + bump to 1.0.1 The 1.0.0 wheel was missing upload_paths.py: it was absent from the [tool.setuptools] py-modules list (and setup.py py_modules), so setuptools never packed it. weknora_mcp_server.py line 26 does `from upload_paths import resolve_upload_file_path, set_active_transport`, so any path that fully imports the module — i.e. the MCP client initialize handshake — crashed with ModuleNotFoundError. `--version` survived only because argparse exits before that import runs. Fix: add 'upload_paths' to py-modules (pyproject.toml + setup.py) and bump to 1.0.1 (1.0.0 is immutable on PyPI). Also sync the user-facing version strings (__version__, server_version, main --version) to 1.0.1. * ci(mcp-server): add workflow.yml to build & publish to PyPI via OIDC Adds .github/workflows/workflow.yml adapted from annopick/tuomin's ci.yml: - test: matrix Python 3.10–3.13 (matches requires-python >=3.10), runs the flat test_*.py suite in mcp-server/ - build: on mcp-server-v* tags, builds sdist + wheel via uv build, and guards against the 1.0.0 regression by asserting upload_paths.py is in the wheel before publishing - publish: PyPI Trusted Publishing (OIDC, id-token: write) to weknora-mcp, no API token needed Publishing is gated on tags named mcp-server-v* to decouple from the Go release workflow that already targets v* tags. * fix(mcp-server): route diagnostics to stderr and fix packaging/CI Prevent MCP stdio startup crashes by sending entry-script diagnostics to stderr, package upload_paths in the wheel, and add unittest-based CI with stdout purity regression tests. --------- Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:23:09 +00:00
print(f"服务器运行错误: {e}", file=sys.stderr)
2025-09-08 05:14:35 +00:00
if args.verbose:
import traceback
2025-09-08 05:14:35 +00:00
traceback.print_exc()
sys.exit(1)
2025-09-08 05:14:35 +00:00
def sync_main():
"""同步版本的主函数,用于 entry_points"""
asyncio.run(main())
2025-09-08 05:14:35 +00:00
if __name__ == "__main__":
asyncio.run(main())