Compare commits

..
10 Commits
23 changed files with 383 additions and 207 deletions

No files matched your search

+3 -4
View File
@@ -135,8 +135,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
@@ -183,8 +182,8 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. Im Browser über Port 3000 öffnen
* **Admin‑Login**: http://{IP}:3000/admin-signin
4. Im Browser über Port 8000 öffnen
* **Admin‑Login**: http://{IP}:8000/admin-signin
* **Benutzer**: admin
* **Passwort**: 123456789
</details>
+1 -2
View File
@@ -136,8 +136,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
+3 -4
View File
@@ -135,8 +135,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
@@ -183,8 +182,8 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. Accédez à l’interface sur le port 3000
* **Connexion admin** : http://{IP}:3000/admin-signin
4. Accédez à l’interface sur le port 8000
* **Connexion admin** : http://{IP}:8000/admin-signin
* **Utilisateur** : admin
* **Mot de passe** : 123456789
</details>
+3 -4
View File
@@ -135,8 +135,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
@@ -183,8 +182,8 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. ブラウザでポート 3000 にアクセス
* **管理画面ログイン**: http://{IP}:3000/admin-signin
4. ブラウザでポート 8000 にアクセス
* **管理画面ログイン**: http://{IP}:8000/admin-signin
* **管理ユーザー**: admin
* **管理パスワード**: 123456789
</details>
+3 -4
View File
@@ -135,8 +135,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
@@ -183,8 +182,8 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. 브라우저에서 3000 포트로 접속
* **관리자 로그인 주소**: http://{IP}:3000/admin-signin
4. 브라우저에서 8000 포트로 접속
* **관리자 로그인 주소**: http://{IP}:8000/admin-signin
* **관리자 계정**: admin
* **관리자 비밀번호**: 123456789
</details>
+2 -2
View File
@@ -100,7 +100,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
支持系统包括:
- CentOS 7.9 / 8.5
- Ubuntu 20.04 / 22.04
- Ubuntu 20.04 / 22.04 / 24.04
- Debian 12.4
- 阿里云 Linux 2.1903 / 3.2104
@@ -125,7 +125,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
docker-compose up -d
```
4. 打开浏览器访问 `http://localhost:3000` 使用平台
4. 打开浏览器访问 `http://localhost:8000` 使用平台
</details>
+3 -4
View File
@@ -136,8 +136,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
@@ -184,8 +183,8 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. 瀏覽器透過 3000 埠連線
* **管理員登入**:http://{IP}:3000/admin-signin
4. 瀏覽器透過 8000 埠連線
* **管理員登入**:http://{IP}:8000/admin-signin
* **帳號**:admin
* **密碼**:123456789
</details>
+14 -7
View File
@@ -141,8 +141,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80" # Map port 8000 of the container to port 80 of the host, if you want to access XPack-MCP-Market in your browser at port 8000
depends_on:
- xpack-mysql
- xpack-redis
@@ -190,9 +189,9 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
<br>
4. Access `XPack-MCP-Market` in your browser at port 3000
4. Access `XPack-MCP-Market` in your browser at port 8000
* **Admin User Login Address**: http://{IP}:3000/admin
* **Admin User Login Address**: http://{IP}:8000/admin
* **Admin User**: admin
* **Admin Password**: 123456789
</details>
@@ -213,8 +212,16 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
> Requirements:
> - Node >= 22.x
> - Pnpm >= 10.x
1. Execute the frontend build script
1. Configure environment variable NEXT_PUBLIC_API_URL:
```
vi frontend/.env
```
File content:
```
NEXT_PUBLIC_API_URL=http://127.0.0.1:8001
```
2. Execute the frontend build script
```
cd scripts && ./frontend_build.sh && cd ../
```
@@ -270,7 +277,7 @@ curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
```
### Docker Build
```
docker build -t xpack-mcp-market --build-arg APP=xpack-mcp-market --build-arg VERSION=1.0.0 -f ./scripts/Dockerfile ./
docker build -t xpack-mcp-market --build-arg APP=xpack-mcp-market --build-arg VERSION=1.1.0 -f ./scripts/Dockerfile ./
```
</details>
+14 -12
View File
@@ -19,18 +19,20 @@ const nextConfig = {
async redirects() {
return [];
},
// async rewrites() {
// return [
// {
// source: "/api/:path*",
// destination: `${process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"}/api/:path*`,
// },
// {
// source: "/uploads/:path*",
// destination: `${process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"}/uploads/:path*`,
// },
// ];
// },
async rewrites() {
return [
// {
// source: "/api/:path*",
// destination: `${process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"}/api/:path*`,
// },
{
source: "/uploads/:path*",
destination: `${
process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"
}/uploads/:path*`,
},
];
},
// Headers for better performance
async headers() {
+2 -1
View File
@@ -876,5 +876,6 @@
"Password must be at least 6 characters": "Password must be at least 6 characters",
"Email is required": "Email is required",
"No servers available": "No servers available",
"(Deleted)": "(Deleted)"
"(Deleted)": "(Deleted)",
"Transport Type": "Transport Type"
}
@@ -878,5 +878,6 @@
"Password must be at least 6 characters": "密码必须至少6个字符",
"Email is required": "邮箱为必填项",
"No servers available": "没有可用服务",
"(Deleted)": "(已删除)"
"(Deleted)": "(已删除)",
"Transport Type": "传输方式"
}
@@ -13,6 +13,7 @@ import Link from "next/link";
import { Price } from "@/shared/components/marketplace/Price";
import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext";
import { NavigationItem } from "@/shared/components/Navigation";
import { ServerConfig } from "@/shared/components/marketplace/ServerConfig";
interface DetailProps {
product: ServiceData;
@@ -214,40 +215,11 @@ export const Detail: React.FC<DetailProps> = ({
</h3>
</div>
{/* Code Snippet */}
<Card className="w-full max-w-2xl bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<div className="text-left font-mono">
<div className="flex items-center gap-2 mb-2 justify-between">
<Chip className="text-white" size="sm" variant="flat">
{platformConfig?.name} MCP
</Chip>
<Button
isIconOnly
size="sm"
variant="flat"
className="min-w-6 w-6 h-6 p-0 text-white"
onPress={onCopy}
aria-label={t("Copy code")}
>
<Copy size={14} />
</Button>
</div>
<pre className="p-2 sm:p-4 rounded-md text-xs overflow-x-auto">
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"${mcpName}": {
"type": "sse",
"autoApprove":"all",
"url": "${url}?authkey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-Auth-Key}`}</span>
{`"
}
}
}`}
</code>
</pre>
</div>
</Card>
<ServerConfig
mcpName={mcpName}
url={url}
className="w-full max-w-2xl"
/>
</CardBody>
</Card>
</div>
@@ -12,6 +12,7 @@ import { Navigation } from "./Navigation";
import { Footer } from "./Footer";
import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext";
import { NavigationItem } from "@/shared/components/Navigation";
import { ServerConfig } from "@/shared/components/marketplace/ServerConfig";
interface DetailProps {
product: ServiceData;
@@ -174,40 +175,11 @@ export const Detail: React.FC<DetailProps> = ({
</h3>
</div>
{/* Code Snippet */}
<Card className="w-full max-w-2xl bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<div className="text-left font-mono">
<div className="flex items-center gap-2 mb-2 justify-between">
<Chip className="text-white" size="sm" variant="flat">
{platformConfig?.name} MCP
</Chip>
<Button
isIconOnly
size="sm"
variant="flat"
className="min-w-6 w-6 h-6 p-0 text-white"
onPress={onCopy}
aria-label={t("Copy code")}
>
<Copy size={14} />
</Button>
</div>
<pre className="p-2 sm:p-4 rounded-md text-xs overflow-x-auto">
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"${mcpName}": {
"type": "sse",
"autoApprove":"all",
"url": "${url}?authkey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-Auth-Key}`}</span>
{`"
}
}
}`}
</code>
</pre>
</div>
</Card>
<ServerConfig
mcpName={mcpName}
url={url}
className="w-full max-w-2xl"
/>
</CardBody>
</Card>
</div>
@@ -13,6 +13,7 @@ import { Navigation } from "./Navigation";
import { Footer } from "./Footer";
import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext";
import { NavigationItem } from "@/shared/components/Navigation";
import { ServerConfig } from "@/shared/components/marketplace/ServerConfig";
interface DetailProps {
product: ServiceData;
@@ -141,40 +142,11 @@ export const Detail: React.FC<DetailProps> = ({
</h3>
</div>
{/* Code Snippet */}
<Card className="w-full max-w-2xl bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<div className="text-left font-mono">
<div className="flex items-center gap-2 mb-2 justify-between">
<Chip className="text-white" size="sm" variant="flat">
{platformConfig?.name} MCP
</Chip>
<Button
isIconOnly
size="sm"
variant="flat"
className="min-w-6 w-6 h-6 p-0 text-white"
onPress={onCopy}
aria-label={t("Copy code")}
>
<Copy size={14} />
</Button>
</div>
<pre className="p-2 sm:p-4 rounded-md text-xs overflow-x-auto">
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"${mcpName}": {
"type": "sse",
"autoApprove":"all",
"url": "${url}?authkey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-Auth-Key}`}</span>
{`"
}
}
}`}
</code>
</pre>
</div>
</Card>
<ServerConfig
mcpName={mcpName}
url={url}
className="w-full max-w-2xl"
/>
</CardBody>
</Card>
</div>
+6 -34
View File
@@ -12,6 +12,7 @@ import { Navigation } from "./Navigation";
import { Footer } from "./Footer";
import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext";
import { NavigationItem } from "@/shared/components/Navigation";
import { ServerConfig } from "@/shared/components/marketplace/ServerConfig";
interface DetailProps {
product: ServiceData;
@@ -161,40 +162,11 @@ export const Detail: React.FC<DetailProps> = ({
>
<div className="p-6">
{/* Code Snippet */}
<Card className="w-full bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<div className="text-left font-mono">
<div className="flex items-center gap-2 mb-2 justify-between">
<Chip className="text-white" size="sm" variant="flat">
{platformConfig?.name} MCP
</Chip>
<Button
isIconOnly
size="sm"
variant="flat"
className="min-w-6 w-6 h-6 p-0 text-white"
onPress={onCopy}
aria-label={t("Copy code")}
>
<Copy size={14} />
</Button>
</div>
<pre className="p-2 sm:p-4 rounded-md text-xs overflow-x-auto">
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"${mcpName}": {
"type": "sse",
"autoApprove":"all",
"url": "${url}?authkey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-Auth-Key}`}</span>
{`"
}
}
}`}
</code>
</pre>
</div>
</Card>
<ServerConfig
mcpName={mcpName}
url={url}
className="w-full max-w-2xl"
/>
</div>
</Tab>
</Tabs>
@@ -1,7 +1,7 @@
"use client";
import React from "react";
import { Card, Chip, Button } from "@nextui-org/react";
import React, { useState } from "react";
import { Card, Chip, Button, Select, SelectItem } from "@nextui-org/react";
import { FlipButton } from "@/shared/components/FlipButton";
import { useAuth } from "@/shared/lib/useAuth";
import { useTranslation } from "react-i18next";
@@ -13,23 +13,38 @@ import toast from "react-hot-toast";
interface ServerConfigProps {
mcpName?: string;
url?: string;
className?: string;
}
type McpType = "sse" | "streamable-http";
export const ServerConfig: React.FC<ServerConfigProps> = ({
mcpName,
url = process.env.NEXT_PUBLIC_MCP_URL,
className = "md:min-w-[40%] w-[40%] max-w-100 mt-12",
}) => {
const { t } = useTranslation();
const { handleLogin } = useAuth();
const { platformConfig } = usePlatformConfig();
const [mcpType, setMcpType] = useState<McpType>("sse");
const mcpTypeOptions = [
{ key: "sse", label: "SSE (Server-Sent Events)" },
{ key: "streamable-http", label: "Streamable HTTP" },
];
const getCodeContent = () => {
const serverUrl =
mcpType === "streamable-http"
? `${url}/streamable-http?authkey={Your-${platformConfig?.name}-Auth-Key}`
: `${url}?authkey={Your-${platformConfig?.name}-Auth-Key}`;
return `{
"mcpServers": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"type": "${mcpType}",
"autoApprove":"all",
"url": "${url}?authkey={Your-${platformConfig?.name}-Auth-Key}"
"url": "${serverUrl}"
}
}
}`;
@@ -45,8 +60,8 @@ export const ServerConfig: React.FC<ServerConfigProps> = ({
};
return (
<div className="min-w-full md:min-w-[40%] w-[40%] max-w-100 flex-shrink-0 mt-12">
<div className="mb-4 mt-1">
<div className={`min-w-full ${className} flex-shrink-0`}>
<div className="mb-2 mt-1">
<FlipButton
className="w-full rounded-md"
text={t("Sign up to get the auth key")}
@@ -54,8 +69,35 @@ export const ServerConfig: React.FC<ServerConfigProps> = ({
/>
</div>
{/* MCP Type Selection */}
<div className="mb-2">
<Select
label={t("Transport Type")}
placeholder={t("Select MCP transport type")}
selectedKeys={[mcpType]}
onSelectionChange={(keys) => {
const selectedKey = Array.from(keys)[0] as McpType;
setMcpType(selectedKey);
}}
className="w-full"
size="sm"
classNames={{
trigger: " bg-black text-white data-[hover=true]:bg-dark-200",
label: "!text-default-200",
value: "!text-white",
popoverContent: "bg-black text-white",
}}
>
{mcpTypeOptions.map((option) => (
<SelectItem key={option.key} value={option.key}>
{option.label}
</SelectItem>
))}
</Select>
</div>
{/* Code Snippet */}
<Card className="w-full max-w-2xl bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<Card className="w-full bg-black text-white p-2 sm:p-4 rounded-lg relative z-10">
<div className="text-left font-mono">
<div className="flex items-center gap-2 mb-2 justify-between">
<Chip className="text-white" size="sm" variant="flat">
@@ -77,9 +119,12 @@ export const ServerConfig: React.FC<ServerConfigProps> = ({
{`{
"mcpServers": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"type": "${mcpType}",
"autoApprove":"all",
"url": "${url}?authkey=`}
"url": "`}
{mcpType === "streamable-http"
? `${url}/streamable-http?authkey=`
: `${url}?authkey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-Auth-Key}`}</span>
{`"
}
+1 -2
View File
@@ -26,8 +26,7 @@ services:
networks:
- xpack
ports:
- "3000:3000"
- "8002:8002"
- "8000:80"
depends_on:
- xpack-mysql
- xpack-redis
+3 -2
View File
@@ -39,7 +39,7 @@ MYSQL_DATA_MOUNT="/var/lib/xpack/mysql"
REDIS_PASSWORD="redis_6sJZDm"
REDIS_MAP_PORT=6379
XPACK_MCP_MARKET_MAP_PORT=3000
XPACK_MCP_MARKET_MAP_PORT=8000
RABBITMQ_USER="rabbitmq"
RABBITMQ_PASSWORD="rabbitmq_Gs123dA"
@@ -1064,8 +1064,9 @@ operate() {
Install_Deb_Pack
fi
write_ip_env
sleep 5s
#sleep 5s
echo_info "start docker..."
systemctl reset-failed docker.service
systemctl start docker
echo_info "start network..."
+1 -1
View File
@@ -147,7 +147,7 @@ app.add_middleware(AuthMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
+239 -9
View File
@@ -8,12 +8,16 @@ from datetime import datetime, timezone
from starlette.requests import Request
from starlette.responses import Response
import asyncio
import time
from mcp.server.sse import SseServerTransport
from mcp.server.streamable_http import StreamableHTTPServerTransport
from mcp.server.lowlevel import Server
from services.api_service.services.mcp_server_factory import McpServerFactory
from services.common.logging_config import get_logger
from services.api_service.repositories.user_apikey_repository import UserApiKeyRepository
from services.api_service.utils.connection_manager import connection_manager
from services.common.database import get_db
from services.common.config import Config
logger = get_logger(__name__)
@@ -23,7 +27,17 @@ class McpController:
def __init__(self):
self.sse = SseServerTransport("/messages/")
# StreamableHTTP 会话持久化:按 (service_id, user_id) 维度保存传输与服务器任务
self._http_transports: dict[str, StreamableHTTPServerTransport] = {}
self._http_server_tasks: dict[str, asyncio.Task] = {}
self._http_servers: dict[str, Server] = {}
self.server_factory = McpServerFactory()
# 会话回收:空闲时间记录与后台清理任务
self._session_last_activity: dict[str, float] = {}
self._session_gc_task: Optional[asyncio.Task] = None
self._session_gc_stop_event = asyncio.Event()
self._session_idle_ttl = getattr(Config, "MCP_SESSION_IDLE_TTL_SECONDS", 900)
self._session_cleanup_interval = getattr(Config, "MCP_SESSION_CLEANUP_INTERVAL_SECONDS", 60)
async def handle_sse_connection(self, request: Request):
"""Handle MCP Streamable HTTP SSE connection with resume capability."""
@@ -114,9 +128,8 @@ class McpController:
async def handle_sse_connection_asgi(self, request: Request):
"""Return an ASGI app that handles MCP SSE connections.
Starlette's Route will call this with a Request and then treat
the returned callable as an ASGI app. We implement the SSE handshake
and streaming within the returned ASGI callable using scope/receive/send.
Starlette 的 Route 会传入一个 Request,然后将返回的可调用对象视为 ASGI 应用。
我们在返回的 ASGI 可调用中完成 SSE 握手,并运行 MCP 服务器。
"""
async def asgi(scope, receive, send):
@@ -160,10 +173,6 @@ class McpController:
user_id, apikey_id = user_info
# Get apikey for logging (extract first 10 characters for audit)
apikey = req.query_params.get("apikey", "") or req.query_params.get("authkey", "")
apikey_for_log = apikey[:10] if apikey else None
# Create MCP server instance (pass user_id and apikey_id for billing and logging)
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
@@ -195,8 +204,6 @@ class McpController:
# Unregister connection
connection_manager.unregister_connection(connection_key)
# End of ASGI callable; no further Response needed
except ConnectionError as e:
logger.warning(f"Connection error - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}")
response_body = b"Connection error"
@@ -224,6 +231,185 @@ class McpController:
return asgi
async def handle_streamable_http_asgi(self, request: Request):
"""Return an ASGI app that handles MCP Streamable HTTP (GET/POST/DELETE).
将请求交由StreamableHTTP的handle_request处理;同时使用持久化的transport与server,
让 GET/POST/DELETE 在同一会话中交互,保证工具/资源列表等请求能返回到相同的 SSE 流。
"""
async def asgi(scope, receive, send):
req = Request(scope)
client_ip = req.client.host if req.client else "unknown"
user_agent = req.headers.get("user-agent", "unknown")
service_id = None
session_key = None
try:
# Extract service_id from URL path (supports both ID and slug_name)
service_id = self._extract_service_id(req)
if not service_id:
response_body = b"Missing service_id parameter or service not found"
await send({
'type': 'http.response.start',
'status': 400,
'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
logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
# 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
session_key = f"{service_id}:{user_id}"
# Register connection to manager(便于统计与审计)
connection_key = connection_manager.register_connection(service_id, user_id, client_ip)
try:
# 获取(或创建)持久化的 StreamableHTTP 传输与 MCP 服务器
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
# 记录会话活跃
self._note_session_activity(session_key)
# 将请求交由 transport 处理;若是 GET,将建立 SSE;若是 POST,将发送 JSON-RPC;DELETE 将终止会话
await transport.handle_request(scope, receive, send)
# 处理完成后更新活跃时间(避免长处理后的误清理)
self._note_session_activity(session_key)
# 如果会话已经终止,立即清理资源
if transport.is_terminated or req.method == "DELETE":
await self._cleanup_http_session(session_key)
finally:
# Unregister connection(按请求粒度解除登记)
connection_manager.unregister_connection(connection_key)
except ConnectionError as e:
logger.warning(f"Connection error - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}")
response_body = b"Connection error"
await send({
'type': 'http.response.start',
'status': 503,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
except Exception as e:
logger.error(f"StreamableHTTP handling failed - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}", exc_info=True)
response_body = f"Internal server error: {str(e)}".encode('utf-8')
await send({
'type': 'http.response.start',
'status': 500,
'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 asgi
async def _ensure_http_session(self, session_key: str, service_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport:
"""Ensure a persistent StreamableHTTP transport and MCP server exist for the session.
- 创建(或复用) transport
- 若尚未启动服务器,则在 transport.connect() 上启动 mcp_server.run()
"""
if session_key in self._http_transports:
transport = self._http_transports[session_key]
# 启动回收任务(若未启动)
self._ensure_session_gc_task()
# 记录活跃
self._note_session_activity(session_key)
# 服务器任务存在且未结束,直接复用
task = self._http_server_tasks.get(session_key)
if task and not task.done():
return transport
# 没有任务或任务已结束,则重新启动
mcp_server = self._http_servers.get(session_key)
if mcp_server is None:
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
self._http_servers[session_key] = mcp_server
init_options = mcp_server.create_initialization_options()
init_options.server_name = f"mcp-service-{service_id}"
async def run_server():
async with transport.connect() as (read_stream, write_stream):
try:
await mcp_server.run(read_stream, write_stream, init_options)
except asyncio.CancelledError:
logger.info(f"MCP server task cancelled - Service: {service_id}, User: {user_id}")
except Exception as e:
logger.error(f"MCP server task error: {e}", exc_info=True)
task = asyncio.create_task(run_server())
self._http_server_tasks[session_key] = task
return transport
# 新建 transport 与 server
transport = StreamableHTTPServerTransport(mcp_session_id=None, is_json_response_enabled=False)
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
self._http_transports[session_key] = transport
self._http_servers[session_key] = mcp_server
init_options = mcp_server.create_initialization_options()
init_options.server_name = f"mcp-service-{service_id}"
logger.info(f"StreamableHTTP session created - Service: {service_id}, User: {user_id}")
# 启动回收任务(若未启动)并记录活跃
self._ensure_session_gc_task()
self._note_session_activity(session_key)
async def run_server():
async with transport.connect() as (read_stream, write_stream):
try:
await mcp_server.run(read_stream, write_stream, init_options)
except asyncio.CancelledError:
logger.info(f"MCP server task cancelled - Service: {service_id}, User: {user_id}")
except Exception as e:
logger.error(f"MCP server task error: {e}", exc_info=True)
task = asyncio.create_task(run_server())
self._http_server_tasks[session_key] = task
return transport
async def _cleanup_http_session(self, session_key: str) -> None:
"""Cleanup transport and server task for a terminated session."""
task = self._http_server_tasks.pop(session_key, None)
transport = self._http_transports.pop(session_key, None)
self._http_servers.pop(session_key, None)
if task and not task.done():
try:
# 终止 transport,促使服务器退出
if transport and not transport.is_terminated:
await transport.terminate()
await task
except Exception:
logger.exception("Error while waiting for server task to finish")
def _extract_service_id(self, request: Request) -> Optional[str]:
"""Extract service ID from request path, supporting both ID and slug_name."""
service_identifier = request.path_params.get("service_id")
@@ -312,3 +498,47 @@ class McpController:
def get_sse_mount_handler(self):
"""Get SSE message handler for processing requests."""
return self.sse.handle_post_message
def get_streamable_http_handler(self):
# 不再返回持久化实例的 handler;如果确实需要,可改为在路由层调用 handle_streamable_http_asgi
# 这里保留占位以避免外部引用报错,但不使用。
raise NotImplementedError("Use handle_streamable_http_asgi for StreamableHTTP routes")
def _ensure_session_gc_task(self) -> None:
"""Start background GC task if not already running."""
if self._session_gc_task is None or self._session_gc_task.done():
loop = asyncio.get_running_loop()
self._session_gc_task = loop.create_task(self._run_session_gc())
logger.info("Session GC task started")
def _note_session_activity(self, session_key: str) -> None:
"""Update last activity timestamp for a session."""
self._session_last_activity[session_key] = time.time()
async def _run_session_gc(self) -> None:
"""Background task to recycle idle or terminated sessions."""
try:
while not self._session_gc_stop_event.is_set():
await asyncio.sleep(self._session_cleanup_interval)
now = time.time()
stale_keys: list[str] = []
for key, transport in list(self._http_transports.items()):
last = self._session_last_activity.get(key, 0)
idle = (now - last) > self._session_idle_ttl
task = self._http_server_tasks.get(key)
active_task = task is not None and not task.done()
# 仅在无活动任务时才按TTL清理,避免中断活跃的SSE会话
if transport.is_terminated or (idle and not active_task):
stale_keys.append(key)
for key in stale_keys:
logger.info(f"Recycling session {key} (terminated or idle > {self._session_idle_ttl}s)")
try:
await self._cleanup_http_session(key)
except Exception:
logger.exception("Error during session cleanup")
self._session_last_activity.pop(key, None)
except asyncio.CancelledError:
pass
finally:
logger.info("Session GC task stopped")
+4 -3
View File
@@ -102,7 +102,7 @@ app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # MCP clients may come from different domains
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"], # Add OPTIONS for preflight requests
allow_methods=["GET", "POST", "DELETE", "OPTIONS"], # 添加 DELETE 支持 StreamableHTTP 终止
allow_headers=["*"],
expose_headers=["*"], # Expose all response headers for SSE reconnection
)
@@ -197,8 +197,9 @@ def mcp_connections_stats():
# Create MCP Streamable HTTP routes
# Use Starlette sub-app to handle MCP protocol's underlying SSE connections
mcp_routes = [
Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]),
Mount("/messages/", app=mcp.get_sse_mount_handler()),
Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]), # 保留原有 SSE GET
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 路由
]
mcp_app = Starlette(routes=mcp_routes)
@@ -50,6 +50,11 @@ class McpServerFactory:
"""Return available tools list for this service"""
return await self._handle_list_tools(service_id)
# Register resources list handler (optional, return empty when not configured)
@app.list_resources()
async def list_resources() -> List[types.Resource]:
return []
# Register tool call handler
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> List[types.Content]:
+2
View File
@@ -73,3 +73,5 @@ class Config:
"/uploads/",
"/api/common/"
]
MCP_SESSION_IDLE_TTL_SECONDS = int(os.getenv("MCP_SESSION_IDLE_TTL_SECONDS", 900))
MCP_SESSION_CLEANUP_INTERVAL_SECONDS = int(os.getenv("MCP_SESSION_CLEANUP_INTERVAL_SECONDS", 60))