WeKnora/mcp-server
annopick 400bbbf717
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 21:23:09 +08:00
..
.gitignore feat: 添加了配套mcp服务器包 2025-09-08 19:43:45 +08:00
CHANGELOG.md fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
Dockerfile fix(security): close remaining GHSA gaps from security triage 2026-07-02 15:03:53 +08:00
EXAMPLES.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
INSTALL.md fix python dependency version for mcp 2025-10-30 18:08:47 +08:00
LICENSE feat: 添加了配套mcp服务器包 2025-09-08 19:43:45 +08:00
MANIFEST.in feat: 添加了配套mcp服务器包 2025-09-08 19:43:45 +08:00
MCP_CONFIG.md fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
PROJECT_SUMMARY.md refactor(terminology): rename user-facing tenant labels to workspace 2026-07-14 16:14:43 +08:00
README.md fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
__init__.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
main.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
pyproject.toml fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
requirements.txt feat(mcp-server): add multi-transport support (stdio / SSE / HTTP) 2026-05-29 16:40:29 +08:00
run.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
run_server.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
setup.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
test_file_path_security.py fix(security): apply MCP upload sandbox when transport is set via CLI 2026-07-02 17:08:17 +08:00
test_imports.py Refactor code for improved readability and consistency 2025-12-01 18:15:39 +08:00
test_module.py feat(mcp): expose read-only wiki tools via Python MCP server 2026-05-29 16:37:12 +08:00
test_stdio_stdout.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00
upload_paths.py fix(security): apply MCP upload sandbox when transport is set via CLI 2026-07-02 17:08:17 +08:00
weknora_mcp_server.py fix(mcp-server): route diagnostics to stderr to fix stdio protocol crash (#2371) 2026-07-28 21:23:09 +08:00

README.md

WeKnora MCP Server

这是一个 Model Context Protocol (MCP) 服务器,提供对 WeKnora 知识管理 API 的访问。

快速开始

推荐直接参考 MCP配置说明,无需进行以下操作。

1. 安装依赖

pip install -r requirements.txt

2. 配置环境变量

# Linux/macOS
export WEKNORA_BASE_URL="http://localhost:8080/api/v1"
export WEKNORA_API_KEY="your_api_key_here"

# Windows PowerShell
$env:WEKNORA_BASE_URL="http://localhost:8080/api/v1"
$env:WEKNORA_API_KEY="your_api_key_here"

# Windows CMD
set WEKNORA_BASE_URL=http://localhost:8080/api/v1
set WEKNORA_API_KEY=your_api_key_here

3. 运行服务器

推荐方式 - 使用主入口点:

python main.py

其他运行方式:

# 使用原始启动脚本
python run_server.py

# 使用便捷脚本
python run.py

# 直接运行服务器模块
python weknora_mcp_server.py

# 作为 Python 模块运行
python -m weknora_mcp_server

4. 命令行选项

python main.py --help                 # 显示帮助信息
python main.py --check-only           # 仅检查环境配置
python main.py --verbose              # 启用详细日志
python main.py --version              # 显示版本信息

安装为 Python 包

从 PyPI 安装

pip install weknora-mcp

PyPI 包名为 weknora-mcp(请勿与第三方包 weknora-mcp-server 混淆)。 安装后命令行入口仍为 weknora-mcp-server / weknora-server。

开发模式安装

pip install -e .

安装后可以使用命令行工具:

weknora-mcp-server
# 或
weknora-server

生产模式安装

pip install .

构建分发包

# 使用 setuptools
python setup.py sdist bdist_wheel

# 使用现代构建工具
pip install build
python -m build

测试模组

运行测试脚本验证模组是否正常工作:

python test_module.py

功能特性

该 MCP 服务器提供以下工具:

空间管理

  • create_tenant - 创建新空间
  • list_tenants - 列出所有空间

知识库管理

  • create_knowledge_base - 创建知识库
  • list_knowledge_bases - 列出知识库
  • get_knowledge_base - 获取知识库详情
  • delete_knowledge_base - 删除知识库
  • hybrid_search - 混合搜索

知识管理

  • create_knowledge_from_url - 从 URL 创建知识
  • list_knowledge - 列出知识
  • get_knowledge - 获取知识详情
  • delete_knowledge - 删除知识

模型管理

  • create_model - 创建模型
  • list_models - 列出模型
  • get_model - 获取模型详情

会话管理

  • create_session - 创建聊天会话
  • get_session - 获取会话详情
  • list_sessions - 列出会话
  • delete_session - 删除会话

聊天功能

  • chat - 发送聊天消息

块管理

  • list_chunks - 列出知识块
  • delete_chunk - 删除知识块

故障排除

如果遇到导入错误,请确保:

  1. 已安装所有必需的依赖包
  2. Python 版本兼容(推荐 3.10+)
  3. 没有文件名冲突(避免使用 mcp.py 作为文件名)

调用效果

118d078426f42f3d4983c13386085d7f