Compare commits

...
3 Commits
Author SHA1 Message Date
Dot.L 2fd6272bbd Update release.yaml 2026-10-26 19:02:45 +08:00
Liujian 470d53cad6 feat(mcp): support MCP 0728 stateless JSON-RPC protocol and pin mcp SDK version
- Implement stateless HTTP JSON-RPC request handling (discover, tools/list, tools/call)
- Support extracting apikey from Authorization and X-Api-Key headers
- Pin mcp dependency to '>=1.12.0,<2.0.0' to prevent breaking changes in v2.x
- Update docker-compose image tag and volume config
2026-10-26 18:57:48 +08:00
Liujian f2b61bae7d Change the mysql-connector-python dependency to pymysql 2026-05-18 11:00:07 +08:00
9 changed files with 227 additions and 49 deletions

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ jobs:
node-version: '22.16' node-version: '22.16'
- name: Pnpm install and build - name: Pnpm install and build
run: | run: |
npm install -g pnpm npm install -g pnpm@9
./scripts/frontend_build.sh ./scripts/frontend_build.sh
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
+1
View File
@@ -14,6 +14,7 @@ python-multipart
google-auth>=2.0.0 google-auth>=2.0.0
google-auth-oauthlib>=1.0.0 google-auth-oauthlib>=1.0.0
fastapi_mcp fastapi_mcp
mcp>=1.12.0,<2.0.0
alipay-sdk-python>=3.0.0 alipay-sdk-python>=3.0.0
wechatpayv3[async] wechatpayv3[async]
cryptography>=41.0.0 cryptography>=41.0.0
+10 -11
View File
@@ -3,7 +3,7 @@ import sys
import re import re
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv from dotenv import load_dotenv
import mysql.connector import pymysql
from typing import Optional, List, Tuple, Union from typing import Optional, List, Tuple, Union
# 加载环境变量 # 加载环境变量
@@ -29,12 +29,13 @@ def get_db_connection(db_config: Optional[DBConfig] = None):
if db_config is None: if db_config is None:
db_config = DBConfig() db_config = DBConfig()
return mysql.connector.connect( return pymysql.connect(
host=db_config.MYSQL_HOST, host=db_config.MYSQL_HOST,
port=db_config.MYSQL_PORT, port=db_config.MYSQL_PORT,
user=db_config.MYSQL_USER, user=db_config.MYSQL_USER,
password=db_config.MYSQL_PASSWORD, password=db_config.MYSQL_PASSWORD,
database=db_config.MYSQL_DB database=db_config.MYSQL_DB,
charset='utf8mb4'
) )
def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]: def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]:
@@ -50,19 +51,17 @@ def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]:
conn = get_db_connection(db_config) conn = get_db_connection(db_config)
cursor = conn.cursor() cursor = conn.cursor()
# 查询version配置
cursor.execute("SELECT value FROM sys_config WHERE `key` = 'version'") cursor.execute("SELECT value FROM sys_config WHERE `key` = 'version'")
result = cursor.fetchone() result = cursor.fetchone()
cursor.close() cursor.close()
conn.close() conn.close()
if result and len(result) > 0: if result:
# 将元组的第一个元素转换为字符串并返回 return str(result[0])
return str(result['value'] if isinstance(result, dict) else result[0])
return None return None
except mysql.connector.Error as err: except pymysql.Error as err:
# 如果表不存在或其他数据库错误,返回None # 如果表不存在或其他数据库错误,返回None
print(f"查询版本信息失败: {err}") print(f"查询版本信息失败: {err}")
return None return None
@@ -124,7 +123,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
try: try:
cursor.execute(sql) cursor.execute(sql)
print(f" -> 成功") print(f" -> 成功")
except mysql.connector.Error as e: except pymysql.Error as e:
print(f" -> SET语句执行错误: {e}") print(f" -> SET语句执行错误: {e}")
# SET语句失败不影响后续执行 # SET语句失败不影响后续执行
continue continue
@@ -144,7 +143,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
try: try:
cursor.execute(sql) cursor.execute(sql)
print(f" -> 成功") print(f" -> 成功")
except mysql.connector.Error as e: except pymysql.Error as e:
if "Duplicate column name" in str(e): if "Duplicate column name" in str(e):
print(f" -> 忽略重复列错误: {e}") print(f" -> 忽略重复列错误: {e}")
continue continue
@@ -166,7 +165,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
except FileNotFoundError: except FileNotFoundError:
print(f"错误: SQL 文件不存在: {file_path}") print(f"错误: SQL 文件不存在: {file_path}")
return False return False
except mysql.connector.Error as err: except pymysql.Error as err:
print(f"数据库错误: {err}") print(f"数据库错误: {err}")
if sql: if sql:
print(f"出错的SQL语句: {sql}") print(f"出错的SQL语句: {sql}")
+1
View File
@@ -66,6 +66,7 @@ CREATE TABLE IF NOT EXISTS `user_apikey` (
`expire_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Expiration timestamp', `expire_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Expiration timestamp',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation timestamp', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation timestamp',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last update timestamp', `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last update timestamp',
`is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT 'Logical deletion: 0 (normal), 1 (deleted)',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE INDEX `uk_apikey` (`apikey`) COMMENT 'Unique API key index', UNIQUE INDEX `uk_apikey` (`apikey`) COMMENT 'Unique API key index',
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
+5
View File
@@ -0,0 +1,5 @@
ALTER TABLE `user_apikey` ADD COLUMN `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT 'Logical deletion: 0 (normal), 1 (deleted)' AFTER `updated_at`;
INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`)
VALUES ('xpack-version','version', '1.4.0', 'Add is_deleted column to user_apikey', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP;
+1 -1
View File
@@ -5,7 +5,7 @@
"downloads": { "downloads": {
"program": { "program": {
"xpack_mcp_market": { "xpack_mcp_market": {
"version": "v1.4.1", "version": "v1.4.5",
"docker": "xpackai/xpack-mcp-market", "docker": "xpackai/xpack-mcp-market",
"tar": "xpack-mcp-market.tar.gz", "tar": "xpack-mcp-market.tar.gz",
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz" "url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
+170 -35
View File
@@ -5,6 +5,7 @@ Supports both service_id (UUID) and slug_name service identifiers
from typing import Optional from typing import Optional
from datetime import datetime, timezone from datetime import datetime, timezone
import json
from sqlalchemy import false from sqlalchemy import false
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response
@@ -165,6 +166,10 @@ class McpController:
logger.info(f"Received SSE connection request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...") logger.info(f"Received SSE connection request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
# Extract user ID (for billing) - must provide valid apikey # Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req) user_info = self._extract_user_info(req)
if not user_info: if not user_info:
@@ -288,6 +293,9 @@ class McpController:
logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...") logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
# Security: validate Origin to prevent DNS rebinding (MCP spec requirement) # Security: validate Origin to prevent DNS rebinding (MCP spec requirement)
origin = req.headers.get('origin') origin = req.headers.get('origin')
if origin and not self._is_origin_allowed(origin, req): if origin and not self._is_origin_allowed(origin, req):
@@ -304,9 +312,42 @@ class McpController:
await send({'type': 'http.response.body', 'body': response_body}) await send({'type': 'http.response.body', 'body': response_body})
return return
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules # Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req)
if not user_info:
response_body = b"Missing or invalid apikey parameter"
await send({
'type': 'http.response.start',
'status': 401,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
user_id, apikey_id = user_info
if not self._check_invoke_permission(user_id, service_id):
response_body = b"Invoke permission denied"
await send({
'type': 'http.response.start',
'status': 403,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# MCP 0728 (2026-07-28) Stateless Handler for POST JSON-RPC requests
if req.method == 'POST' and ('application/json' in content_type or req.headers.get("mcp-method") or req.headers.get("mcp-protocol-version")):
stateless_handled = await self._handle_stateless_mcp_request(req, send, service_id, user_id, apikey_id)
if stateless_handled:
return
# Validate Accept & Content-Type headers according to legacy MCP Streamable HTTP rules
accept = (req.headers.get('accept') or '').lower() accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
if req.method == 'GET': if req.method == 'GET':
if 'text/event-stream' not in accept: if 'text/event-stream' not in accept:
logger.warning("Missing Accept: text/event-stream for GET SSE") logger.warning("Missing Accept: text/event-stream for GET SSE")
@@ -350,34 +391,6 @@ class McpController:
}) })
await send({'type': 'http.response.body', 'body': response_body}) await send({'type': 'http.response.body', 'body': response_body})
return return
# Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req)
if not user_info:
response_body = b"Missing or invalid apikey parameter"
await send({
'type': 'http.response.start',
'status': 401,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
user_id, apikey_id = user_info
if not self._check_invoke_permission(user_id, service_id):
response_body = b"Invoke permission denied"
await send({
'type': 'http.response.start',
'status': 403,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
raw_client_id = req.headers.get('x-client-instance-id') or req.headers.get('x-client-id') raw_client_id = req.headers.get('x-client-instance-id') or req.headers.get('x-client-id')
client_instance_id = (re.sub(r'[^a-zA-Z0-9._-]', '', raw_client_id)[:32]) if raw_client_id else None client_instance_id = (re.sub(r'[^a-zA-Z0-9._-]', '', raw_client_id)[:32]) if raw_client_id else None
ua = user_agent or "" ua = user_agent or ""
@@ -679,14 +692,22 @@ class McpController:
def _extract_user_info(self, request: Request) -> Optional[tuple[str, str]]: def _extract_user_info(self, request: Request) -> Optional[tuple[str, str]]:
"""Extract user ID and apikey ID from request by validating apikey parameter.""" """Extract user ID and apikey ID from request by validating apikey parameter or headers."""
# Get apikey from URL query parameters # Get apikey from URL query parameters or Headers
apikey = request.query_params.get("apikey") apikey = request.query_params.get("apikey")
if not apikey: if not apikey:
apikey = request.query_params.get("authkey") apikey = request.query_params.get("authkey")
if not apikey: if not apikey:
logger.warning("Apikey not found in URL parameters") auth_header = request.headers.get("authorization", "")
return None if auth_header.startswith("Bearer "):
apikey = auth_header[7:].strip()
elif auth_header:
apikey = auth_header.strip()
if not apikey:
apikey = request.headers.get("x-api-key")
if not apikey:
logger.warning("Apikey not found in URL parameters or Headers")
return None
logger.debug(f"Validating apikey: {apikey[:10]}...") # Only log first 10 characters for debugging logger.debug(f"Validating apikey: {apikey[:10]}...") # Only log first 10 characters for debugging
@@ -775,3 +796,117 @@ class McpController:
pass pass
finally: finally:
logger.info("Session GC task stopped") logger.info("Session GC task stopped")
async def _handle_stateless_mcp_request(
self,
req: Request,
send,
service_id: str,
user_id: str,
apikey_id: str
) -> bool:
"""
Handle MCP 0728 (2026-07-28) stateless HTTP JSON-RPC request.
Returns True if request was handled directly, False if it should fallback to legacy session transport.
"""
mcp_method_hdr = req.headers.get("mcp-method")
protocol_hdr = req.headers.get("mcp-protocol-version")
try:
body_bytes = await req.body()
if not body_bytes:
return False
payload = json.loads(body_bytes.decode("utf-8"))
except Exception:
return False
if not isinstance(payload, dict):
return False
method = mcp_method_hdr or payload.get("method")
req_id = payload.get("id")
if not method:
return False
logger.info(f"Handling MCP 0728 stateless request - Service ID: {service_id}, Method: {method}, ReqID: {req_id}")
result = None
error = None
try:
if method in ("server/discover", "discover"):
result = await self.server_factory.get_server_discover(service_id)
elif method in ("tools/list", "list_tools"):
tools = await self.server_factory._handle_list_tools(service_id)
result = {
"tools": [t.model_dump(by_alias=True, exclude_none=True) for t in tools]
}
elif method in ("tools/call", "call_tool"):
params = payload.get("params") or {}
tool_name = req.headers.get("mcp-name") or params.get("name")
arguments = params.get("arguments") or {}
if not tool_name:
error = {"code": -32602, "message": "Missing tool name in params or header"}
else:
contents, extra = await self.server_factory.execute_tool(
service_id, tool_name, arguments, user_id, apikey_id
)
content_list = []
for c in contents:
if hasattr(c, "model_dump"):
content_list.append(c.model_dump(by_alias=True, exclude_none=True))
elif isinstance(c, dict):
content_list.append(c)
else:
content_list.append({"type": "text", "text": str(c)})
result = {
"content": content_list,
"isError": False
}
elif method == "initialize":
result = {
"protocolVersion": "2026-07-28",
"capabilities": {
"tools": {
"listChanged": False,
"ttlMs": 3600000,
"cacheScope": "service"
}
},
"serverInfo": {
"name": f"mcp-service-{service_id}",
"version": "1.0.0"
}
}
elif method in ("notifications/initialized", "ping"):
result = {}
elif method in ("resources/list", "prompts/list"):
result = {method.split("/")[0]: []}
else:
error = {"code": -32601, "message": f"Method not found: {method}"}
except Exception as e:
logger.error(f"Error handling stateless MCP method {method}: {str(e)}", exc_info=True)
error = {"code": -32603, "message": f"Internal error: {str(e)}"}
response_payload = {
"jsonrpc": "2.0",
"id": req_id
}
if error is not None:
response_payload["error"] = error
else:
response_payload["result"] = result
resp_bytes = json.dumps(response_payload).encode("utf-8")
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'application/json; charset=utf-8'],
[b'content-length', str(len(resp_bytes)).encode()],
[b'mcp-protocol-version', b'2026-07-28'],
],
})
await send({'type': 'http.response.body', 'body': resp_bytes})
return True
+1 -1
View File
@@ -199,7 +199,7 @@ def mcp_connections_stats():
mcp_routes = [ mcp_routes = [
Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]), # 保留原有 SSE GET Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]), # 保留原有 SSE GET
Mount("/messages/", app=mcp.get_sse_mount_handler()), # 保留原有 SSE POST 挂载 Mount("/messages/", app=mcp.get_sse_mount_handler()), # 保留原有 SSE POST 挂载
Route("/{service_id}/streamable-http", endpoint=mcp.handle_streamable_http_asgi, methods=["GET", "POST", "DELETE"]), # 新增 StreamableHTTP 路由 Route("/{service_id}/streamable-http", endpoint=mcp.handle_streamable_http_asgi, methods=["GET", "POST", "DELETE"]), # StreamableHTTP 路由
] ]
mcp_app = Starlette(routes=mcp_routes) mcp_app = Starlette(routes=mcp_routes)
@@ -70,6 +70,43 @@ class McpServerFactory:
logger.info("MCP server instance created successfully") logger.info("MCP server instance created successfully")
return app return app
async def execute_tool(
self,
service_id: str,
name: str,
arguments: dict,
user_id: str,
apikey_id: Optional[str] = None
) -> tuple[List[types.Content], dict]:
"""
Execute tool directly with billing for stateless 0728 protocol calls.
"""
return await self._handle_call_tool_with_billing(service_id, name, arguments, user_id, apikey_id)
async def get_server_discover(self, service_id: str) -> dict:
"""
Return server discovery metadata complying with MCP 0728 (2026-07-28) spec.
Includes ttlMs and cacheScope for tools list caching.
"""
tools = await self._handle_list_tools(service_id)
return {
"protocolVersion": "2026-07-28",
"capabilities": {
"tools": {
"listChanged": False,
"ttlMs": 3600000, # Cache 1 hour
"cacheScope": "service"
},
"resources": {},
"prompts": {}
},
"serverInfo": {
"name": f"mcp-service-{service_id}",
"version": "1.0.0"
},
"tools": [tool.model_dump(by_alias=True, exclude_none=True) for tool in tools]
}
async def _handle_list_tools(self, service_id: str) -> List[types.Tool]: async def _handle_list_tools(self, service_id: str) -> List[types.Tool]:
""" """
Handle tools list query Handle tools list query