Compare commits

..
26 Commits
Author SHA1 Message Date
Dot.L 523265e550 Merge pull request #58 from xpack-ai/feature/mcp
Payment information encryption and  wait for the initial connection to initialize successfully before processing the request
2025-11-14 18:11:01 +08:00
Liujian 5d2db84d75 update env example 2025-11-14 17:47:31 +08:00
Liujian 4826859152 Payment information encryption 2025-11-14 16:58:08 +08:00
Liujian 92f8b68f0e A new connection recovery mechanism has been added in the event of a network outage. 2025-11-14 15:50:39 +08:00
Liujian 87a4b6e2de Wait for the initial connection to initialize successfully before processing the request. 2025-11-14 15:50:00 +08:00
Dot.L 58d30344f5 Merge pull request #57 from xpack-ai/feature/mcp
Completed: OutputSchema functionality added to the MCP tools list.
2025-11-07 17:23:54 +08:00
Liujian a6064035bd Completed: OutputSchema functionality added to the MCP tools list. 2025-11-07 17:23:24 +08:00
Dot.L 141fb4758f Merge pull request #56 from xpack-ai/feature/mcp
fix bug: API enabled status error when updating service Swagger docum…
2025-11-07 16:56:07 +08:00
Liujian 5450027bbc fix bug: API enabled status error when updating service Swagger documentation 2025-11-07 16:53:25 +08:00
riverLethe 721a213bbf fix:hidden server length in marketpalce card panel 2025-11-07 14:03:38 +08:00
Dot.L 9ce81f5012 Merge pull request #55 from xpack-ai/feature/mcp
MCP tool list response adds outputSchema
2025-11-07 10:32:07 +08:00
Liujian a28f3bb680 MCP tool list response adds outputSchema 2025-11-07 10:31:29 +08:00
Dot.L 14e45235b4 Merge pull request #54 from xpack-ai/feature/synchronize_saas
update mcp session gc logic
2025-10-23 17:55:51 +08:00
Liujian 6a69d678fd update mcp session gc logic 2025-10-23 17:55:24 +08:00
Dot.L 48ba7ffd96 Merge pull request #53 from xpack-ai/feature/synchronize_saas
Translate Chinese into English
2025-10-22 23:21:23 +08:00
Liujian faa1f552e6 Translate Chinese into English 2025-10-22 23:20:57 +08:00
riverLethe b2bd2e30dd feat/ui:The server configuration allows switching between Streamable HTTP and SSE. 2025-10-22 19:59:49 +08:00
Dot.L e25e119ba1 Merge pull request #52 from xpack-ai/feature/synchronize_saas
support streamable http
2025-10-22 19:14:26 +08:00
Liujian eb1811fa3a support streamable http 2025-10-22 19:01:11 +08:00
Dot.L ee65da1665 Merge pull request #51 from xpack-ai/feature/synchronize_saas
Update Deployment Tutorial
2025-10-20 18:22:42 +08:00
Liujian bda95f756a Update Deployment Tutorial 2025-10-20 17:15:03 +08:00
Liujian ae7b6d20e4 Update Deployment Tutorial 2025-10-20 17:11:52 +08:00
Liujian 0d1dff05d6 Merge remote-tracking branch 'origin/main' into feature/synchronize_saas 2025-10-20 15:17:02 +08:00
riverLethe 434518b2b3 feat/op:uploads path use rewrite 2025-10-20 14:57:33 +08:00
Dot.L 3321efc3d4 Merge pull request #50 from xpack-ai/feature/synchronize_saas
Update Deployment Tutorial
2025-10-20 13:42:35 +08:00
Liujian 0313ced503 Update Deployment Tutorial 2025-10-20 09:19:15 +08:00
40 changed files with 965 additions and 269 deletions

No files matched your search

+8 -2
View File
@@ -6,7 +6,7 @@ MYSQL_PASSWORD=mysql_ZTdhRB
MYSQL_DB=xpack
# 服务端口配置
API_PORT=8000
API_PORT=8002
ADMIN_PORT=8001
# 调试配置
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/
RABBITMQ_VHOST=/
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default
+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>
@@ -162,7 +162,7 @@ export const Home: React.FC<HomeProps> = ({
<CardBody className="p-4">
{/* Header */}
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-2 w-full">
<div className="flex items-center gap-2 w-full overflow-hidden">
<div className="w-8 h-8 bg-slate-600 rounded-sm flex items-center justify-center">
<Star className="w-4 h-4 text-white" />
</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>
@@ -168,7 +168,7 @@ export const Home: React.FC<HomeProps> = ({
<CardBody className="p-4">
{/* Header */}
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-2">
<div className="flex items-center gap-2 overflow-hidden">
<div className="w-8 h-8 bg-blue-100 rounded-lg flex items-center justify-center">
<Zap className="w-4 h-4 text-blue-600" />
</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>
{`"
}
+2 -1
View File
@@ -15,4 +15,5 @@ google-auth>=2.0.0
google-auth-oauthlib>=1.0.0
fastapi_mcp
alipay-sdk-python>=3.0.0
wechatpayv3[async]
wechatpayv3[async]
cryptography>=41.0.0
+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..."
+9 -3
View File
@@ -6,12 +6,12 @@ MYSQL_PASSWORD=mysql_ZTdhRB
MYSQL_DB=xpack
# 服务端口配置
API_PORT=8000
API_PORT=8002
ADMIN_PORT=8001
# 调试配置
DEBUG=false
LOG_LEVEL=INFO
LOG_LEVEL=DEBUG
# Redis 配置
REDIS_HOST=xpack-redis
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/
RABBITMQ_VHOST=/
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default
@@ -0,0 +1,81 @@
"""
One-time migration: encrypt plaintext payment channel configs at rest.
Usage:
python scripts/resource/migrate_encrypt_payment_configs.py [--dry-run] [--limit N] [--ids alipay,wechat,stripe]
Requirements:
- Environment variable PAYMENT_CONFIG_SECRET_KEY must be set to a base64-encoded 32-byte key.
- cryptography library installed.
"""
import argparse
import sys
from typing import List
from datetime import datetime, timezone
from services.common.database import SessionLocal
from services.common.models.payment_channel import PaymentChannel
from services.common.utils.secure_config import is_encrypted, decrypt_config, encrypt_config
def migrate_configs(dry_run: bool = False, limit: int = 0, only_ids: List[str] | None = None) -> int:
db = SessionLocal()
migrated = 0
try:
query = db.query(PaymentChannel)
if only_ids:
query = query.filter(PaymentChannel.id.in_(only_ids))
rows = query.all()
if limit and len(rows) > limit:
rows = rows[:limit]
for row in rows:
raw = row.config
if not raw:
continue
if is_encrypted(raw):
# already encrypted
continue
cfg = decrypt_config(raw)
if cfg is None:
# cannot parse plaintext json; skip but report
print(f"[WARN] Skip {row.id}: config not JSON or decrypt failed")
continue
try:
enc = encrypt_config(cfg)
except Exception as e:
print(f"[ERROR] Encrypt {row.id} failed: {e}")
continue
if dry_run:
print(f"[DRY-RUN] Would encrypt {row.id}")
else:
row.config = enc
row.updated_at = datetime.now(timezone.utc)
db.add(row)
migrated += 1
if not dry_run and migrated:
db.commit()
print(f"Done. Migrated {migrated} rows. Dry-run={dry_run}")
return migrated
finally:
db.close()
def main():
parser = argparse.ArgumentParser(description="Encrypt legacy payment channel configs")
parser.add_argument("--dry-run", action="store_true", help="Do not write changes, only report")
parser.add_argument("--limit", type=int, default=0, help="Limit number of rows to process")
parser.add_argument("--ids", type=str, default="", help="Comma-separated channel ids to process")
args = parser.parse_args()
only_ids = [x.strip() for x in args.ids.split(",") if x.strip()] if args.ids else None
try:
migrate_configs(dry_run=args.dry_run, limit=args.limit, only_ids=only_ids)
except Exception as e:
print(f"Migration failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
+2 -2
View File
@@ -34,11 +34,11 @@ fi
# 初始化数据库
python ./init_db.py
nohup uvicorn services.admin_service.main:app --host 0.0.0.0 --port 8001 --reload > ${LOG_DIR}/admin_service.log 2>&1 &
nohup uvicorn services.admin_service.main:app --host 0.0.0.0 --port 8001 --reload --timeout-graceful-shutdown 2 --timeout-keep-alive 1 > ${LOG_DIR}/admin_service.log 2>&1 &
sleep 5s
nohup uvicorn services.api_service.main:app --host 0.0.0.0 --port 8002 --reload > ${LOG_DIR}/api_service.log 2>&1 &
nohup uvicorn services.api_service.main:app --host 0.0.0.0 --port 8002 --reload --timeout-graceful-shutdown 2 --timeout-keep-alive 1 > ${LOG_DIR}/api_service.log 2>&1 &
HOSTNAME=""
cd frontend/ && nohup node server.js> ${LOG_DIR}//frontend.log 2>&1 &
+1 -1
View File
@@ -5,7 +5,7 @@
"downloads": {
"program": {
"xpack_mcp_market": {
"version": "v1.1.0",
"version": "v1.1.1",
"docker": "xpackai/xpack-mcp-market",
"tar": "xpack-mcp-market.tar.gz",
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
@@ -1,10 +1,11 @@
from fastapi import APIRouter, Depends, Query, Body
from pydantic import config
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.payment_channel_service import PaymentChannelService
from services.common.utils.secure_config import decrypt_config, encrypt_config
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
import json
router = APIRouter()
@@ -27,10 +28,9 @@ async def payment_channel_list(
result = []
for item in list:
enable = item.status == 1
try:
cfg = json.loads(item.config)
except:
cfg = {}
cfg = {}
if item.config:
cfg = decrypt_config(item.config) or {}
result.append(
{
"id": item.id,
@@ -56,14 +56,16 @@ async def payment_channel_enable(
if data:
if id == "alipay":
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask
alipay_monitor_task = AlipayOrderMonitorTask.get_instance()
alipay_monitor_task.start_monitor()
monitor = OrderMonitorTask.get_instance()
# Ensure monitor is running and enable Alipay client
monitor.start_monitor()
monitor.enable_client("alipay")
cfg = decrypt_config(data.config) if data.config else {}
return ResponseUtils.success(
{
"id": id,
"name": data.name,
"config": data.config,
"config": cfg or {},
"is_enabled": data.status == 1,
"updated_time": data.updated_at,
}
@@ -83,14 +85,15 @@ async def payment_channel_disable(
data = payment_channel_service.update_status(id, 0)
if data:
if id == "alipay":
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask
alipay_monitor_task = AlipayOrderMonitorTask.get_instance()
alipay_monitor_task.stop_monitor()
monitor = OrderMonitorTask.get_instance()
# Disable Alipay client without stopping global monitor
monitor.disable_client("alipay")
cfg = decrypt_config(data.config) if data.config else {}
return ResponseUtils.success(
{
"id": id,
"name": data.name,
"config": data.config,
"config": cfg or {},
"is_enabled": data.status == 1,
"updated_time": data.updated_at,
}
@@ -109,8 +112,12 @@ async def payment_channel_config(
return ResponseUtils.error("Payment channel id cannot be empty")
config = body.get("config")
if config:
configStr = json.dumps(config)
data = payment_channel_service.update_config(id=id, config=configStr)
# Encrypt config before persisting to DB
try:
encrypted = encrypt_config(config)
except Exception as e:
return ResponseUtils.error(f"Failed to encrypt config: {str(e)}")
data = payment_channel_service.update_config(id=id, config=encrypted)
if data:
# Convert str to object
return ResponseUtils.success(
+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=["*"],
)
@@ -172,8 +172,8 @@ class McpManagerService:
new_api.response_examples = temp_api.response_examples
new_api.response_headers = temp_api.response_headers
new_api.operation_examples = temp_api.operation_examples
new_api.enabled = temp_api.enabled
new_api.is_deleted = temp_api.is_deleted
new_api.enabled = True
# new_api.is_deleted = temp_api.is_deleted
# Save new API record
self.mcp_tool_api_repository.create(new_api)
@@ -6,6 +6,7 @@ from services.common.database import SessionLocal
from datetime import datetime
from services.admin_service.repositories.payment_channel_repository import PaymentChannelRepository
from services.common.models.payment_channel import PaymentChannel
from services.common.utils.secure_config import decrypt_config
logger = logging.getLogger(__name__)
@@ -35,13 +36,14 @@ class PaymentChannelService:
channel_config = self.payment_channel_repository.payment_channel_get(id)
if channel_config is None:
return None
# Support encrypted-at-rest config
config = {}
if channel_config.config is not None:
try:
config = json.loads(channel_config.config)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse payment channel config JSON: {str(e)}")
if channel_config.config:
cfg = decrypt_config(channel_config.config)
if cfg is None:
logger.error("Failed to decrypt or parse payment channel config")
return None
config = cfg
config["enable"] = channel_config.status == 1
config["id"] = channel_config.id
return config
@@ -64,19 +66,17 @@ class PaymentChannelService:
logger.warning("Stripe payment channel config is empty")
return None
# Parse config JSON
try:
config = json.loads(stripe_channel.config)
# Validate required fields
if not config.get("secret") or not config.get("webhook_secret"):
logger.warning("Stripe config missing required fields: secret or webhook_secret")
return None
logger.info("Successfully retrieved Stripe config")
return config
except json.JSONDecodeError as e:
logger.error(f"Failed to parse Stripe config JSON: {str(e)}")
# Decrypt/parse config JSON
config = decrypt_config(stripe_channel.config)
if config is None:
logger.error("Failed to decrypt Stripe config")
return None
# Validate required fields
if not config.get("secret") or not config.get("webhook_secret"):
logger.warning("Stripe config missing required fields: secret or webhook_secret")
return None
logger.info("Successfully retrieved Stripe config")
return config
except Exception as e:
logger.error(f"Failed to get Stripe config: {str(e)}")
+395 -9
View File
@@ -8,12 +8,17 @@ 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
from services.api_service.utils.mcp_event_store import RedisEventStore
logger = get_logger(__name__)
@@ -23,7 +28,17 @@ class McpController:
def __init__(self):
self.sse = SseServerTransport("/messages/")
# StreamableHTTP session persistence: store transport and server task keyed by (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()
# Session recycling: track idle time and run a background cleanup task
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 +129,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 passes a Request and treats the returned callable as an ASGI app.
The returned ASGI callable performs the SSE handshake and runs the MCP server.
"""
async def asgi(scope, receive, send):
@@ -160,10 +174,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 +205,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 +232,339 @@ 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).
Delegate requests to StreamableHTTP's handle_request; use persistent transport and server
so GET/POST/DELETE interact within the same session, ensuring tools/resources queries
return to the same SSE stream.
"""
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]}...")
# Security: validate Origin to prevent DNS rebinding (MCP spec requirement)
origin = req.headers.get('origin')
if origin and not self._is_origin_allowed(origin, req):
logger.warning(f"Rejected by Origin validation - Origin: {origin}, Host: {req.url.scheme}://{req.url.netloc}")
response_body = b"Forbidden: invalid Origin"
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
# 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()
if req.method == 'GET':
if 'text/event-stream' not in accept:
logger.warning("Missing Accept: text/event-stream for GET SSE")
response_body = b"Not Acceptable: require text/event-stream"
await send({
'type': 'http.response.start',
'status': 406,
'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
elif req.method == 'POST':
# Client should list both application/json and text/event-stream
if not ('application/json' in accept and 'text/event-stream' in accept):
logger.warning("POST Accept must include application/json and text/event-stream")
response_body = b"Not Acceptable: require application/json and text/event-stream"
await send({
'type': 'http.response.start',
'status': 406,
'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
# Body must be JSON-RPC payload
if 'application/json' not in content_type:
logger.warning("Unsupported Media Type for POST; require application/json")
response_body = b"Unsupported Media Type: require application/json"
await send({
'type': 'http.response.start',
'status': 415,
'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
# 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}"
# Build connection key for lifecycle management
connection_key = f"{service_id}:{user_id}:{client_ip}"
try:
# Get or create a persistent StreamableHTTP transport and MCP server
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
logger.debug(f'method: {req.method},request body: {req.body}')
# Note session activity
self._note_session_activity(session_key)
# Ensure transport is connected before delegating (avoid race on first request)
try:
await self._wait_transport_ready(transport, timeout_seconds=2.0)
except TimeoutError:
logger.error("Transport not ready within timeout; refusing request")
response_body = b"Service Unavailable: transport not ready"
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})
return
# Register only on GET (SSE handshake); POST just updates activity
if req.method == "GET":
connection_manager.register_connection(service_id, user_id, client_ip)
logger.debug(f'register connection successful.connection_key is {connection_key}')
elif req.method == "POST":
connection_manager.update_activity(connection_key)
# Delegate request to transport; GET establishes SSE; POST sends JSON-RPC; DELETE terminates the session
await transport.handle_request(scope, receive, send)
# Update activity after handling (avoid cleaning long-running processing)
self._note_session_activity(session_key)
# If the session has terminated, clean up and unregister
if transport.is_terminated:
await self._cleanup_http_session(session_key)
connection_manager.unregister_connection(connection_key)
# For explicit DELETE, ensure termination cleanup
if req.method == "DELETE" and not transport.is_terminated:
try:
await transport.terminate()
except Exception:
logger.exception("Error terminating transport on DELETE")
await self._cleanup_http_session(session_key)
connection_manager.unregister_connection(connection_key)
finally:
# Unregister when SSE GET disconnects
if req.method == "GET":
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 _wait_transport_ready(self, transport: StreamableHTTPServerTransport, timeout_seconds: float = 2.0) -> None:
"""Wait until StreamableHTTP transport is connected and streams are initialized.
The underlying transport sets internal streams in connect(); POST/GET handling
requires these to exist. We poll briefly to avoid race on first request.
"""
deadline = time.time() + timeout_seconds
# Access internal attributes defensively; break once read writer is present
while True:
# If terminated, break early
if transport.is_terminated:
raise TimeoutError("Transport terminated during wait")
# Heuristic: writer/reader set once connect() started
has_read_writer = getattr(transport, "_read_stream_writer", None) is not None
has_write_reader = getattr(transport, "_write_stream_reader", None) is not None
if has_read_writer and has_write_reader:
return
if time.time() > deadline:
raise TimeoutError("Transport not ready")
await asyncio.sleep(0.01)
def _is_origin_allowed(self, origin: str, req: Request) -> bool:
"""Validate Origin header against request host to prevent DNS rebinding.
Policy:
- If Origin is present, it must match the scheme+host of the request URL.
- Allow common localhost variants (127.0.0.1, localhost) across http/https.
- If Origin is missing, treat as allowed (native clients may omit it).
"""
try:
if not origin:
return True
origin = origin.strip()
request_host = f"{req.url.scheme}://{req.url.netloc}"
if origin == request_host:
return True
allowed_local_origins = {
"http://127.0.0.1",
f"http://127.0.0.1:{req.url.port}" if req.url.port else "http://127.0.0.1",
"http://localhost",
f"http://localhost:{req.url.port}" if req.url.port else "http://localhost",
"https://127.0.0.1",
f"https://127.0.0.1:{req.url.port}" if req.url.port else "https://127.0.0.1",
"https://localhost",
f"https://localhost:{req.url.port}" if req.url.port else "https://localhost",
}
return origin in allowed_local_origins
except Exception:
logger.exception("Origin validation error")
return False
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.
- Create or reuse the transport
- If the server has not been started, start mcp_server.run() within transport.connect()
"""
if session_key in self._http_transports:
transport = self._http_transports[session_key]
# Start GC task if not started
self._ensure_session_gc_task()
# Note activity
self._note_session_activity(session_key)
# If the server task exists and is not finished, reuse it
task = self._http_server_tasks.get(session_key)
if task and not task.done():
return transport
# If no task or the task is finished, restart
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
# Create transport and server
event_store = RedisEventStore(namespace=session_key, ttl_seconds=Config.MCP_SESSION_IDLE_TTL_SECONDS)
transport = StreamableHTTPServerTransport(
mcp_session_id=None,
is_json_response_enabled=False,
event_store=event_store,
)
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}")
# Start GC task if not started and note activity
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:
# Terminate transport to prompt server exit
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 +653,48 @@ 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):
# We no longer return a handler for a persistent instance; if needed, call
# handle_streamable_http_asgi at the routing layer. This placeholder is
# retained to avoid external references breaking, but it is not used.
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()
# Only TTL-clean when there is no active task, to avoid killing active SSE sessions
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,15 +50,20 @@ 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]:
async def call_tool(name: str, arguments: dict) -> tuple[List[types.Content], dict]:
"""Execute specified tool"""
# user_id must exist, otherwise we shouldn't reach here
if not user_id:
error_msg = "Missing user authentication"
logger.error(error_msg)
return [types.TextContent(type="text", text=error_msg)]
return [types.TextContent(type="text", text=error_msg)], {}
return await self._handle_call_tool_with_billing(service_id, name, arguments, user_id, apikey_id)
@@ -97,7 +102,7 @@ class McpServerFactory:
finally:
db.close()
async def _handle_call_tool_with_billing(self, service_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> List[types.Content]:
async def _handle_call_tool_with_billing(self, service_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> tuple[List[types.Content], dict]:
"""
Handle tool call with billing logic
@@ -140,7 +145,7 @@ class McpServerFactory:
# If tool API id not found, skip sending billing message to avoid FK errors
if not api_id_for_log:
return [types.TextContent(type="text", text=error_msg)]
return [types.TextContent(type="text", text=error_msg)], {}
call_log = ApiCallLogInfo(
user_id=user_id,
@@ -158,7 +163,7 @@ class McpServerFactory:
)
await self.billing_service.send_billing_message(call_log, False, datetime.now(timezone.utc))
return [types.TextContent(type="text", text=error_msg)]
return [types.TextContent(type="text", text=error_msg)],{}
logger.info(f"Pre-deduction successful - User ID: {user_id}, Deduction amount: {pre_deduct_result.service_price}")
amount = pre_deduct_result.service_price
@@ -166,6 +171,7 @@ class McpServerFactory:
db = next(get_db())
call_success = False
result: List[types.ContentBlock] = []
response_data: dict = {}
# calculate input token amount
input_token_amount = Decimal("0")
output_token_amount = Decimal("0")
@@ -190,7 +196,7 @@ class McpServerFactory:
logger.error(error_msg)
# Close DB and return error without sending billing message to avoid FK violation
db.close()
return [types.TextContent(type="text", text=error_msg)]
return [types.TextContent(type="text", text=error_msg)],{}
try:
logger.info(f"Found tool configuration: {tool_config.name}")
@@ -200,7 +206,7 @@ class McpServerFactory:
logger.debug(f"Call params: {call_params}")
# Execute tool
result = await self.tool_service.execute_tool(tool_config, arguments, call_params)
result,response_data = await self.tool_service.execute_tool(tool_config, arguments, call_params)
call_success = True
logger.info(f"Tool call successful - User ID: {user_id}, Tool: {name}")
@@ -249,7 +255,7 @@ class McpServerFactory:
logger.info(f"Billing message sent - User ID: {user_id}, Tool: {name}, Success: {call_success}")
# Ensure return type is correct
return result
return result,response_data
def _create_mcp_service(self, db) -> McpService:
"""
+65 -3
View File
@@ -131,11 +131,15 @@ class McpService:
"required": []
}
return types.Tool(
t = types.Tool(
name=tool_api.name,
description=tool_api.description,
inputSchema=input_schema
inputSchema=input_schema,
)
output_schema = self._build_output_schema(tool_api)
if output_schema:
t.outputSchema = output_schema
return t
except Exception as e:
self.logger.error(f"Tool conversion failed for {tool_api.name}: {e}")
return None
@@ -271,4 +275,62 @@ class McpService:
return {
"base_url": service.base_url or "",
"headers": headers
}
}
def _build_output_schema(self,tool_api:McpToolApi) -> Optional[dict]:
"""
Build output schema based on API configuration
Priority:
1) Use explicit JSON Schema from `response_schema` if present
2) Infer a reasonable JSON Schema from `response_examples`
3) Fallback to a generic text content schema
"""
# 1) Try explicit response schema
try:
if tool_api.response_schema:
parsed = json.loads(tool_api.response_schema)
if isinstance(parsed, dict):
return parsed
except Exception as e:
self.logger.warning(f"Failed to parse response_schema for {tool_api.name}: {e}")
# 2) Try to infer schema from examples
try:
if getattr(tool_api, "response_examples", None) and tool_api.response_examples.strip():
examples = json.loads(tool_api.response_examples)
# If multiple examples provided, use the first for inference
example_value = examples[0] if isinstance(examples, list) and examples else examples
inferred = self._infer_json_schema_from_example(example_value)
if inferred:
return inferred
except Exception as e:
self.logger.warning(f"Failed to infer schema from response_examples for {tool_api.name}: {e}")
# 3) Fallback to a generic text MCP content schema
return None
# Helper: Infer a simple JSON Schema from a Python value
def _infer_json_schema_from_example(self, value) -> dict:
try:
if isinstance(value, str):
return {"type": "string"}
if isinstance(value, bool):
return {"type": "boolean"}
if isinstance(value, int):
return {"type": "integer"}
if isinstance(value, float):
return {"type": "number"}
if isinstance(value, list):
if not value:
return {"type": "array", "items": {}}
# Infer from first element; for heterogeneous arrays this is a best-effort
return {"type": "array", "items": self._infer_json_schema_from_example(value[0])}
if isinstance(value, dict):
props = {k: self._infer_json_schema_from_example(v) for k, v in value.items()}
required = list(value.keys())
return {"type": "object", "properties": props, "required": required}
# Fallback
return {"type": "string"}
except Exception:
return {"type": "string"}
@@ -17,7 +17,7 @@ class McpToolService:
def __init__(self):
self.http_builder = HttpRequestBuilder()
async def execute_tool(self, tool_config, arguments: dict, call_params: dict) -> List[types.Content]:
async def execute_tool(self, tool_config, arguments: dict, call_params: dict) -> tuple[List[types.Content],dict]:
"""
Execute tool call
@@ -37,14 +37,17 @@ class McpToolService:
# Send HTTP request
response_text = await self._send_http_request(request_info)
if response_text:
response_data = json.loads(response_text)
else:
response_data = {}
logger.info("Tool execution completed successfully")
return [types.TextContent(type="text", text=response_text)]
return [types.TextContent(type="text", text=response_text)],response_data
except Exception as e:
error_msg = f"Tool execution failed: {str(e)}"
logger.error(f"Tool execution failed: {error_msg}", exc_info=True)
return [types.TextContent(type="text", text=error_msg)]
return [types.TextContent(type="text", text=error_msg)],{}
async def _send_http_request(self, request_info: Dict[str, Any]) -> str:
"""
@@ -81,6 +84,8 @@ class McpToolService:
raise ValueError(f"Unsupported HTTP method: {method}")
logger.info(f"HTTP response status code: {response.status_code}")
if response.text != "":
logger.debug(f"Response content: {response.text}")
response.raise_for_status()
response_text = response.text
@@ -0,0 +1,91 @@
import json
import asyncio
from typing import List, cast
from mcp.server.streamable_http import (
EventStore,
EventId,
StreamId,
EventMessage,
EventCallback,
)
from mcp.types import JSONRPCMessage
from services.common.redis import redis_client
class RedisEventStore(EventStore):
def __init__(self, namespace: str, ttl_seconds: int = 900):
self.namespace = namespace
self.ttl_seconds = ttl_seconds
# Synchronous helpers; wrap at call sites to avoid blocking
def _rpush(self, key: str, value: str) -> int:
return cast(int, redis_client.client.rpush(key, value))
def _expire(self, key: str, seconds: int) -> None:
try:
redis_client.client.expire(key, int(seconds))
except Exception:
# Ignore expiration errors
pass
def _llen(self, key: str) -> int:
return cast(int, redis_client.client.llen(key))
def _lrange(self, key: str, start: int, end: int) -> List[str]:
raw = redis_client.client.lrange(key, int(start), int(end))
items: List[str] = cast(List[str], raw or [])
return [str(i) for i in items]
def _key(self, stream_id: StreamId) -> str:
return f"xpack:mcp:event:{self.namespace}:{stream_id}"
def _event_id(self, stream_id: StreamId, seq: int) -> EventId:
return f"{stream_id}:{seq}"
async def store_event(self, stream_id: StreamId, message: JSONRPCMessage) -> EventId:
key = self._key(stream_id)
payload = json.dumps(message.model_dump(by_alias=True, exclude_none=True))
# Append to Redis list via thread wrapper to avoid blocking
seq: int = await asyncio.to_thread(self._rpush, key, payload)
# rpush returns new length; sequence index is length-1
await asyncio.to_thread(self._expire, key, self.ttl_seconds)
return self._event_id(stream_id, seq - 1)
async def replay_events_after(
self,
last_event_id: EventId,
send_callback: EventCallback,
) -> StreamId | None:
# Parse last_event_id of format "<stream_id>:<seq>"
try:
stream_id_str, seq_str = last_event_id.rsplit(":", 1)
stream_id: StreamId = stream_id_str
start_idx = int(seq_str) + 1
except Exception:
return None
key = self._key(stream_id)
try:
length: int = await asyncio.to_thread(self._llen, key)
except Exception:
# Redis unavailable or key type mismatch; cannot replay
return stream_id
if length <= 0 or start_idx >= length:
return stream_id
# Fetch range [start_idx, length-1]
try:
items: list[str] = await asyncio.to_thread(self._lrange, key, start_idx, length - 1)
except Exception:
return stream_id
for item in items:
try:
data = json.loads(item)
msg = JSONRPCMessage.model_validate(data)
# send_callback is async and expects EventMessage
await send_callback(EventMessage(message=msg, event_id=None))
except Exception:
# Skip malformed entries
continue
return stream_id
+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))
+2 -1
View File
@@ -14,5 +14,6 @@ class PaymentChannel(Base):
id: Mapped[str] = mapped_column(String(36), primary_key=True, autoincrement=False, comment="Primary key")
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Channel name")
status: Mapped[int] = mapped_column(Integer, nullable=False, comment="Channel status, 0: disabled, 1: enabled")
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config")
# Store encrypted JSON envelope for channel configuration (AES-GCM). See secure_config.py
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config (encrypted envelope JSON)")
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="Update time")
+2 -1
View File
@@ -1,4 +1,5 @@
import redis
from redis import Redis
from typing import Optional, Any
from .config import Config
@@ -9,7 +10,7 @@ class RedisClient:
def __init__(self):
"""Initialize Redis connection"""
try:
self.client = redis.Redis(
self.client: Redis = redis.Redis(
host=Config.REDIS_HOST,
port=Config.REDIS_PORT,
password=Config.REDIS_PASSWORD,
+102
View File
@@ -0,0 +1,102 @@
import os
import json
import base64
import secrets
from typing import Optional, Any, Dict
try:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
except Exception:
AESGCM = None
ENC_MARKER = "__enc__"
def _get_key() -> bytes:
"""Load symmetric encryption key from environment.
Expects `PAYMENT_CONFIG_SECRET_KEY` to be a base64-encoded 32-byte key.
Optionally supports `PAYMENT_CONFIG_KEY_ID` for key rotation tracking.
"""
key_b64 = os.getenv("PAYMENT_CONFIG_SECRET_KEY", "")
if not key_b64:
raise RuntimeError("Missing PAYMENT_CONFIG_SECRET_KEY environment variable")
try:
key = base64.b64decode(key_b64)
except Exception as e:
raise RuntimeError("Invalid base64 in PAYMENT_CONFIG_SECRET_KEY") from e
if len(key) != 32:
raise RuntimeError("PAYMENT_CONFIG_SECRET_KEY must decode to 32 bytes (AES-256)")
return key
def is_encrypted(value: Optional[str]) -> bool:
"""Heuristically determine if the stored config string is an encrypted envelope."""
if not value:
return False
try:
obj = json.loads(value)
return isinstance(obj, dict) and obj.get(ENC_MARKER) is True and "ct" in obj and "nonce" in obj
except Exception:
return False
def encrypt_config(config_obj: Dict[str, Any]) -> str:
"""Encrypt config object using AES-GCM and return envelope JSON string.
Envelope format:
{
"__enc__": true,
"v": 1,
"alg": "AESGCM",
"kid": "default",
"nonce": base64,
"ct": base64
}
"""
if AESGCM is None:
raise RuntimeError("cryptography library is required for encryption")
key = _get_key()
aesgcm = AESGCM(key)
nonce = secrets.token_bytes(12) # AES-GCM standard 96-bit nonce
plaintext = json.dumps(config_obj, ensure_ascii=False).encode("utf-8")
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
envelope = {
ENC_MARKER: True,
"v": 1,
"alg": "AESGCM",
"kid": os.getenv("PAYMENT_CONFIG_KEY_ID", "default"),
"nonce": base64.b64encode(nonce).decode("ascii"),
"ct": base64.b64encode(ciphertext).decode("ascii"),
}
return json.dumps(envelope, ensure_ascii=False)
def decrypt_config(value: Optional[str]) -> Optional[Dict[str, Any]]:
"""Decrypt envelope if present; otherwise parse plaintext JSON.
Returns dict on success, or None on failure.
"""
if not value:
return None
# If looks like encrypted envelope, decrypt.
if is_encrypted(value):
if AESGCM is None:
# Cannot decrypt without cryptography
return None
try:
obj = json.loads(value)
key = _get_key()
aesgcm = AESGCM(key)
nonce = base64.b64decode(obj["nonce"])
ct = base64.b64decode(obj["ct"])
plaintext = aesgcm.decrypt(nonce, ct, None)
return json.loads(plaintext.decode("utf-8"))
except Exception:
return None
# Fallback: treat as plaintext JSON
try:
return json.loads(value)
except Exception:
return None