Compare commits

...
44 Commits
Author SHA1 Message Date
jinghui d18adf035b feat(billing): per-service and platform-wide free call limits
release / release (release) Canceled after 0s
- mcp_service.free_daily_limit: per service, per user per day (NULL/0 = unlimited)
- sys_config free_global_daily_limit: applies to every free service
- effective limit = smaller of the two, so a service without its own value
  still inherits the global one
- admin: Pricing tab input, System Settings > Business > Free Call Limits
- submission review pages and endpoints (scaffolding, not yet published end to end)
- allow LAN IPs in http_utils, add docker-compose.yml
2026-11-13 10:20:25 +08:00
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
Dot.L 588f2770b7 Merge pull request #167 from xpack-ai/feature/fix-billing-transaction
Feature/fix billing transaction
2026-04-01 17:23:01 +08:00
Liujian ad743d6ab7 Cancel the _update_wallet_cache method from being included in transactions 2026-03-30 18:17:27 +08:00
Liujian 6145d0b005 _update_wallet_cache is also included in transactions. 2026-03-30 18:01:08 +08:00
Dot.L d147fcb070 Merge pull request #166 from xpack-ai/feature/fix-billing-transaction
Feature/fix billing transaction
2026-03-30 16:40:17 +08:00
Liujian 6196b93e97 The billing processor adds transaction processing and retry mechanisms. 2026-03-30 16:00:58 +08:00
Liujian b51933d625 Fixed the issue where access remained even after the API key was deleted. 2026-03-30 15:59:49 +08:00
Liujian 40ff118798 Handling billing transactions 2026-03-27 18:36:28 +08:00
Dot.L d470677abe Merge pull request #155 from xpack-ai/revert-154-dev
Revert "Dev"
2026-03-11 15:59:28 +08:00
Dot.L 6eac3c6100 Revert "Dev" 2026-03-11 15:56:53 +08:00
Dot.L d22513730a Merge pull request #154 from xpack-ai/dev
Dev
2026-03-11 15:56:36 +08:00
Dot.L 613cece94b Merge pull request #153 from xpack-ai/open-base
user service add update_password
2026-03-11 15:25:00 +08:00
Liujian 5cd1c12323 user service add update_password 2026-03-11 15:13:28 +08:00
Dot.L dcf73b25bf Merge pull request #152 from xpack-ai/open-base
update temp_mcp_service_repository.py
2026-03-11 11:37:35 +08:00
Liujian 64a67e9c8d update temp_mcp_service_repository.py 2026-03-11 11:36:30 +08:00
Dot.L b6160af3c5 Merge pull request #150 from xpack-ai/open-base
mcp service add sort and category_id
2026-03-11 10:57:22 +08:00
Liujian 2ea41dfb75 mcp service add sort and category_id 2026-03-11 10:56:54 +08:00
Dot.L cbcc419f0b Merge pull request #149 from xpack-ai/open-base
update tenant_id type
2026-03-10 18:42:45 +08:00
Liujian 687949beb2 update tenant_id type 2026-03-10 18:41:49 +08:00
Dot.L fedaa5f5df Merge pull request #148 from xpack-ai/open-base
update sys_config index
2026-03-10 16:19:17 +08:00
Liujian c272c87e1f update sys_config index 2026-03-10 16:18:34 +08:00
Dot.L 8a052ff910 Merge pull request #147 from xpack-ai/open-base
The concept of a tenant is added as the foundation of the SaaS code.
2026-03-10 15:37:05 +08:00
Liujian d1c7a4b8c0 The concept of a tenant is added as the foundation of the SaaS code. 2026-03-10 15:33:57 +08:00
Dot.L 1a104338e8 Merge pull request #144 from xpack-ai/feature/1.4-liujian
Feature/1.4 liujian
2026-02-28 16:02:01 +08:00
Liujian 5dd47b2d01 Redis client methods supplement 2026-02-28 15:53:35 +08:00
Dot.L 05c0088f1b Merge pull request #142 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting logic
2026-02-25 15:23:08 +08:00
Dot.L 05b48f3656 Merge pull request #141 from xpack-ai/feature/1.4-liujian
Fix: Removed permission verification from the user information retrie…
2026-02-25 15:18:58 +08:00
Dot.L 5fa1cd741e Merge pull request #140 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting timeout
2026-02-25 11:24:50 +08:00
Dot.L 2cf95fd1c1 Merge pull request #139 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting logic
2026-02-25 11:13:53 +08:00
Dot.L fb23d62fb2 Merge pull request #137 from xpack-ai/feature/1.4-liujian
fix: Handle None message in custom MCP event store during priming event
2026-02-24 15:16:14 +08:00
Dot.L eb4ecff6ed Merge pull request #135 from xpack-ai/feature/1.4-liujian
API adds user role verification
2026-02-14 11:24:00 +08:00
ningyv ffdf931cad Merge pull request #134 from xpack-ai/feature/cx-1.4
chore: upgrade Next.js version
2026-02-06 15:36:06 +08:00
lcx 5a538863b1 Merge remote-tracking branch 'origin/feature/cx-1.4' into dev 2026-02-06 14:49:55 +08:00
Dot.L f6e0130035 Merge pull request #132 from xpack-ai/feature/1.4-liujian
MySQL reconnection mechanism optimization
2026-02-05 13:43:56 +08:00
Dot.L 6f882845c2 Merge pull request #129 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 18:26:34 +08:00
ningyv 5654f83644 Merge pull request #128 from xpack-ai/feature/cx-1.4
feat: 1.4 - List supports search
2026-01-29 18:13:24 +08:00
Dot.L 6d15420b08 Merge pull request #127 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 18:07:56 +08:00
Dot.L 5469eca450 Merge pull request #126 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 17:51:28 +08:00
ningyv 94fd46c8ca Merge pull request #125 from xpack-ai/feature/cx-1.4
feat: 1.4 - List supports search
2026-01-29 17:51:15 +08:00
Dot.L 57bd964d22 Merge pull request #124 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 16:31:00 +08:00
Dot.L aac3a187d1 Merge pull request #123 from xpack-ai/feature/1.4-liujian
Added logic to disable IP login after repeated login failures
2026-01-28 19:18:17 +08:00
42 changed files with 1182 additions and 135 deletions

No files matched your search

+10 -1
View File
@@ -26,8 +26,17 @@ RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/ RABBITMQ_VHOST=/
# Billing queue / retry settings
BILLING_QUEUE_NAME=billing.api.calls
# Max retry attempts (excluding initial consumption)
BILLING_RETRY_TIMES=3
# Base delay seconds for exponential backoff: base * 2^retry_count
BILLING_RETRY_BASE_DELAY_SECONDS=30
# Max delay seconds cap
BILLING_RETRY_MAX_DELAY_SECONDS=1800
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM) # 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())" # Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA= PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification # Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default PAYMENT_CONFIG_KEY_ID=default
+2 -2
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
@@ -43,4 +43,4 @@ jobs:
QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }} QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }}
QINIU_NAME: ${{ secrets.QINIU_NAME }} QINIU_NAME: ${{ secrets.QINIU_NAME }}
run: cd scripts && ./docker_publish.sh ${{ secrets.DOCKER_USERNAME }} upload_qiniu run: cd scripts && ./docker_publish.sh ${{ secrets.DOCKER_USERNAME }} upload_qiniu
+3
View File
@@ -42,3 +42,6 @@ local_settings.py
/.secrets /.secrets
/uploads/ /uploads/
/.vercel/ /.vercel/
# Local deployment data
mysql-data/
+62
View File
@@ -0,0 +1,62 @@
version: '3'
services:
xpack-mysql:
image: mysql:8.0.37
privileged: true
restart: always
container_name: xpack-mysql
hostname: xpack-mysql
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
ports:
- "33306:3306"
environment:
- MYSQL_ROOT_PASSWORD=mysql_ZTdhRB
- MYSQL_DATABASE=xpack
volumes:
- /vol3/1000/HD3/docker/xpack-mcp/mysql-data:/var/lib/mysql
networks:
- xpack
xpack-mcp-market:
image: xpackai/xpack-mcp-market:zhcn
container_name: xpack-mcp-market
privileged: true
restart: always
networks:
- xpack
ports:
- "8010:80"
depends_on:
- xpack-mysql
- xpack-redis
- xpack-rabbitmq
xpack-redis:
container_name: xpack-redis
image: redis:7.2.4
hostname: xpack-redis
privileged: true
restart: always
command:
- bash
- -c
- "redis-server --protected-mode yes --logfile redis.log --appendonly no --port 6379 --requirepass redis_6sJZDm"
networks:
- xpack
xpack-rabbitmq:
container_name: xpack-rabbitmq
image: rabbitmq:4.1.2-alpine
privileged: true
restart: always
environment:
- RABBITMQ_DEFAULT_USER=rabbitmq
- RABBITMQ_DEFAULT_PASS=rabbitmq_Gs123dA
networks:
- xpack
networks:
xpack:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.101.0.0/24
+14 -3
View File
@@ -483,7 +483,6 @@
"Enter server ID": "Enter server ID", "Enter server ID": "Enter server ID",
"Modifying the ID will affect the MCP access URL. Please modify with caution.": "Modifying the ID will affect the MCP access URL. Please modify with caution.", "Modifying the ID will affect the MCP access URL. Please modify with caution.": "Modifying the ID will affect the MCP access URL. Please modify with caution.",
"You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?", "You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?",
"The header name for the API key (e.g., X-API-Key, Authorization)": "The header name for the API key (e.g., X-API-Key, Authorization)", "The header name for the API key (e.g., X-API-Key, Authorization)": "The header name for the API key (e.g., X-API-Key, Authorization)",
"Enter API key": "Enter API key", "Enter API key": "Enter API key",
"Enter bearer token": "Enter bearer token", "Enter bearer token": "Enter bearer token",
@@ -891,5 +890,17 @@
"Auth key name \"{{name}}\" already exists": "Auth key name \"{{name}}\" already exists", "Auth key name \"{{name}}\" already exists": "Auth key name \"{{name}}\" already exists",
"Start date must be before end date": "Start date must be before end date", "Start date must be before end date": "Start date must be before end date",
"Start date cannot be earlier than 30 days ago": "Start date cannot be earlier than 30 days ago", "Start date cannot be earlier than 30 days ago": "Start date cannot be earlier than 30 days ago",
"Resource Groups": "Resource Groups" "Resource Groups": "Resource Groups",
} "Daily call limit per user": "Daily call limit per user",
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited": "Max calls per user per day (Beijing time). Leave blank or 0 for unlimited",
"e.g. 50": "e.g. 50",
"Free Call Limits": "Free Call Limits",
"Global daily call cap for free services": "Global daily call cap for free services",
"Platform-wide daily call limit per user": "Platform-wide daily call limit per user",
"Applies to every free service, leave blank or 0 for unlimited": "Applies to every free service, leave blank or 0 for unlimited",
"If a service also has its own limit, the smaller number wins": "If a service also has its own limit, the smaller number wins",
"Example 100": "Example 100",
"Enter a non-negative integer": "Enter a non-negative integer",
"Free call limit saved": "Free call limit saved",
"Failed to save free call limit": "Failed to save free call limit"
}
+14 -3
View File
@@ -485,7 +485,6 @@
"Enter server ID": "输入服务器ID", "Enter server ID": "输入服务器ID",
"Modifying the ID will affect the MCP access URL. Please modify with caution.": "修改ID会影响MCP的访问地址,需要谨慎修改。", "Modifying the ID will affect the MCP access URL. Please modify with caution.": "修改ID会影响MCP的访问地址,需要谨慎修改。",
"You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "您已修改了服务器ID。这将影响MCP访问地址,可能会破坏现有的集成。您确定要继续吗?", "You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "您已修改了服务器ID。这将影响MCP访问地址,可能会破坏现有的集成。您确定要继续吗?",
"The header name for the API key (e.g., X-API-Key, Authorization)": "API密钥的头部名称(例如:X-API-Key, Authorization)", "The header name for the API key (e.g., X-API-Key, Authorization)": "API密钥的头部名称(例如:X-API-Key, Authorization)",
"Enter API key": "输入API密钥", "Enter API key": "输入API密钥",
"Enter bearer token": "输入Bearer令牌", "Enter bearer token": "输入Bearer令牌",
@@ -957,5 +956,17 @@
"Resource group name \"{{name}}\" already exists": "资源组名称 \"{{name}}\" 已存在", "Resource group name \"{{name}}\" already exists": "资源组名称 \"{{name}}\" 已存在",
"Auth key name \"{{name}}\" already exists": "认证密钥 \"{{name}}\" 已存在", "Auth key name \"{{name}}\" already exists": "认证密钥 \"{{name}}\" 已存在",
"Start date must be before end date": "开始日期必须早于结束日期", "Start date must be before end date": "开始日期必须早于结束日期",
"Start date cannot be earlier than 30 days ago": "开始日期不能早于30天前" "Start date cannot be earlier than 30 days ago": "开始日期不能早于30天前",
} "Daily call limit per user": "每用户每日调用上限",
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited": "每个用户每天最多调用次数(北京时间)。留空或填 0 表示不限",
"e.g. 50": "如 50",
"Free Call Limits": "免费调用限制",
"Global daily call cap for free services": "免费服务的全局每日调用上限",
"Platform-wide daily call limit per user": "全站每用户每日调用上限",
"Applies to every free service, leave blank or 0 for unlimited": "对所有免费服务生效,留空或填 0 表示不限",
"If a service also has its own limit, the smaller number wins": "若单个服务也设置了上限,两者取更小值",
"Example 100": "如 100",
"Enter a non-negative integer": "请输入非负整数",
"Free call limit saved": "免费调用上限已保存",
"Failed to save free call limit": "保存免费调用上限失败"
}
@@ -0,0 +1,55 @@
"use client";
import React, { useEffect, useState } from "react";
export default function AdminSubmissionsPage() {
const [list, setList] = useState<any[]>([]);
const [status, setStatus] = useState("pending");
const load = async () => {
const res = await fetch(`/api/submission/admin/submissions?status=${status}`, { credentials: "include" });
const data = await res.json();
setList(data?.data || []);
};
useEffect(() => { load(); }, [status]);
const approve = async (id: string) => {
await fetch(`/api/submission/admin/submission/${id}/approve`, { method: "PUT", credentials: "include" });
load();
};
const reject = async (id: string) => {
await fetch(`/api/submission/admin/submission/${id}/reject`, { method: "PUT", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ note: "rejected" }) });
load();
};
return (
<div className="max-w-4xl mx-auto p-6">
<h1 className="text-2xl font-bold mb-4">提交审核</h1>
<select value={status} onChange={e => setStatus(e.target.value)} className="border p-2 mb-4">
<option value="pending">待审核</option>
<option value="approved">已通过</option>
<option value="rejected">已拒绝</option>
</select>
<table className="w-full border">
<thead><tr><th>名称</th><th>类型</th><th>状态</th><th>操作</th></tr></thead>
<tbody>
{list.map((s: any) => (
<tr key={s.id} className="border-t">
<td className="p-2">{s.name}</td>
<td className="p-2">{s.type}</td>
<td className="p-2">{s.status}</td>
<td className="p-2">
{s.status === "pending" && (
<>
<button onClick={() => approve(s.id)} className="text-green-600 mr-2">通过</button>
<button onClick={() => reject(s.id)} className="text-red-600">拒绝</button>
</>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
+2 -2
View File
@@ -27,12 +27,12 @@ export default async function RootLayout({
const platformConfig = await getCachedPlatformConfig(); const platformConfig = await getCachedPlatformConfig();
return ( return (
<html lang="en" suppressHydrationWarning data-color-mode="light"> <html lang="zh-CN" suppressHydrationWarning data-color-mode="light">
<head> <head>
<script <script
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: ` __html: `
window.localStorage.setItem("i18nextLng", "${platformConfig?.platform?.language || "en"}"); window.localStorage.setItem("i18nextLng", "${platformConfig?.platform?.language || "zh-CN"}");
`, `,
}} }}
/> />
+40
View File
@@ -0,0 +1,40 @@
"use client";
import React, { useState } from "react";
export default function SubmitPage() {
const [type, setType] = useState("mcp");
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const [file, setFile] = useState<File | null>(null);
const [msg, setMsg] = useState("");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
const fd = new FormData();
fd.append("type", type);
fd.append("name", name);
if (desc) fd.append("description", desc);
if (file) fd.append("file", file);
const res = await fetch("/api/submission/submission", { method: "POST", body: fd, credentials: "include" });
const data = await res.json();
setMsg(JSON.stringify(data));
};
return (
<div className="max-w-xl mx-auto p-6">
<h1 className="text-2xl font-bold mb-4">上传 MCP / Skill / Plugin</h1>
<form onSubmit={handleSubmit} className="space-y-4">
<select value={type} onChange={e => setType(e.target.value)} className="border p-2 w-full">
<option value="mcp">MCP 服务</option>
<option value="skill">AI 技能</option>
<option value="plugin">AI 插件</option>
</select>
<input value={name} onChange={e => setName(e.target.value)} placeholder="名称" className="border p-2 w-full" required />
<textarea value={desc} onChange={e => setDesc(e.target.value)} placeholder="描述" className="border p-2 w-full" />
<input type="file" onChange={e => setFile(e.target.files?.[0] || null)} className="border p-2 w-full" />
<button type="submit" className="bg-blue-600 text-white px-4 py-2 rounded">提交</button>
</form>
{msg && <pre className="mt-4 text-sm">{msg}</pre>}
</div>
);
}
@@ -26,6 +26,7 @@ import {
Captions, Captions,
Code, Code,
CreditCard, CreditCard,
Gauge,
GhostIcon, GhostIcon,
GlobeIcon, GlobeIcon,
LockIcon, LockIcon,
@@ -181,6 +182,12 @@ const SystemSettingsModal: React.FC<SystemSettingsModalProps> = ({
icon: <CreditCard size={16} />, icon: <CreditCard size={16} />,
description: "Configure payment channels", description: "Configure payment channels",
}, },
{
key: SettingModalTab.FREE_QUOTA,
label: "Free Call Limits",
icon: <Gauge size={16} />,
description: "Global daily call cap for free services",
},
{ {
key: SettingModalTab.XPACK_EXPLORE, key: SettingModalTab.XPACK_EXPLORE,
label: "XPack Explore", label: "XPack Explore",
@@ -256,6 +263,7 @@ const SystemSettingsModal: React.FC<SystemSettingsModalProps> = ({
title: "Business", title: "Business",
items: groupItems([ items: groupItems([
SettingModalTab.PAYMENT, SettingModalTab.PAYMENT,
SettingModalTab.FREE_QUOTA,
SettingModalTab.XPACK_EXPLORE, SettingModalTab.XPACK_EXPLORE,
]), ]),
}, },
@@ -31,6 +31,7 @@ import { AdminConfig, EmailConfig } from "@/types/system";
import { LoginSettingsContent } from "./LoginSettingsContent"; import { LoginSettingsContent } from "./LoginSettingsContent";
import { EmailConfigForm } from "@/components/system-setting/EmailConfigForm"; import { EmailConfigForm } from "@/components/system-setting/EmailConfigForm";
import { LanguageSetting } from "@/components/system-setting/LanguageSetting"; import { LanguageSetting } from "@/components/system-setting/LanguageSetting";
import { FreeQuotaSettings } from "@/components/system-setting/FreeQuotaSettings";
import { TagsBarSetting } from "../system-setting/TagsBarSetting"; import { TagsBarSetting } from "../system-setting/TagsBarSetting";
interface SystemSettingsRightContentProps { interface SystemSettingsRightContentProps {
@@ -221,6 +222,8 @@ const SystemSettingsRightContent: React.FC<SystemSettingsRightContentProps> = ({
<PaymentChannelsContent labelPlacement="outside" /> <PaymentChannelsContent labelPlacement="outside" />
)} )}
{section.key === "free-quota" && <FreeQuotaSettings />}
{section.key === "xpack-explore" && ( {section.key === "xpack-explore" && (
<ExplorePanel <ExplorePanel
isEnabled={!!platformConfig.is_showcased} isEnabled={!!platformConfig.is_showcased}
@@ -5,6 +5,7 @@ export enum SettingModalTab {
BRANDING = "branding", BRANDING = "branding",
THEME = "theme", THEME = "theme",
PAYMENT = "payment", PAYMENT = "payment",
FREE_QUOTA = "free-quota",
CODE_INJECTION = "code-injection", CODE_INJECTION = "code-injection",
XPACK_EXPLORE = "xpack-explore", XPACK_EXPLORE = "xpack-explore",
NAVIGATION = "navigation", NAVIGATION = "navigation",
@@ -30,6 +30,7 @@ const _DefaultFormData: MCPServiceFormData = {
auth_token: "", auth_token: "",
charge_type: ChargeType.Free, charge_type: ChargeType.Free,
price: "", price: "",
free_daily_limit: "",
enabled: 0, enabled: 0,
tags: [], tags: [],
apis: [], apis: [],
@@ -46,12 +47,18 @@ const isValidUrl = (url: string) => {
} }
}; };
// Helper function to validate domain format // Helper function to validate domain format (allow domain or IP address including LAN IPs)
const isValidDomain = (url: string) => { const isValidDomain = (url: string) => {
if (!url) return false; if (!url) return false;
try { try {
const parsed = new URL(url); const parsed = new URL(url);
const hostname = parsed.hostname; const hostname = parsed.hostname;
// Allow IPv4 addresses (including private/lan IPs like 192.168.x.x, 10.x.x.x, 172.x.x.x)
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if (ipv4Regex.test(hostname)) return true;
// Allow IPv6 (simplified)
if (hostname.includes(":")) return true;
// Domain format
const domainRegex = /^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,})$/; const domainRegex = /^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,})$/;
return domainRegex.test(hostname); return domainRegex.test(hostname);
} catch { } catch {
@@ -0,0 +1,111 @@
"use client";
import React, { useEffect, useState } from "react";
import { Button, Input } from "@nextui-org/react";
import { fetchAdminAPI } from "@/rpc/admin-api";
import { useTranslation } from "@/shared/lib/useTranslation";
import toast from "react-hot-toast";
interface FreeQuotaResponse {
free_global_daily_limit: number | null;
}
/**
* Platform-wide daily call cap for free services.
*
* Stored in sys_config under free_global_daily_limit. The billing service takes
* the smaller of this value and any per-service limit, so a service with no
* limit of its own still inherits this one.
*/
export const FreeQuotaSettings: React.FC = () => {
const { t } = useTranslation();
const [value, setValue] = useState<string>("");
const [loading, setLoading] = useState<boolean>(true);
const [saving, setSaving] = useState<boolean>(false);
useEffect(() => {
let mounted = true;
(async () => {
const res = await fetchAdminAPI<FreeQuotaResponse>(
"/api/sysconfig/free-quota",
{ method: "GET" }
);
if (!mounted) return;
if (res.success) {
const n = res.data?.free_global_daily_limit;
setValue(n && n > 0 ? String(n) : "");
} else if (res.error_message) {
toast.error(res.error_message);
}
setLoading(false);
})();
return () => {
mounted = false;
};
}, []);
const handleSave = async () => {
const raw = value.trim();
let payload: number | null = null;
if (raw !== "") {
const n = Number(raw);
if (!Number.isInteger(n) || n < 0) {
toast.error(t("Enter a non-negative integer"));
return;
}
payload = n;
}
setSaving(true);
const res = await fetchAdminAPI<FreeQuotaResponse>(
"/api/sysconfig/free-quota",
{ method: "PUT", body: { free_global_daily_limit: payload } as unknown as BodyInit }
);
setSaving(false);
if (res.success) {
const n = res.data?.free_global_daily_limit;
setValue(n && n > 0 ? String(n) : "");
toast.success(t("Free call limit saved"));
} else {
toast.error(res.error_message || t("Failed to save free call limit"));
}
};
return (
<div className="space-y-4">
<Input
type="number"
min={0}
step={1}
labelPlacement="outside"
label={t("Platform-wide daily call limit per user")}
placeholder={t("Example 100")}
value={value}
isDisabled={loading || saving}
onValueChange={setValue}
description={t(
"Applies to every free service, leave blank or 0 for unlimited"
)}
/>
<p className="text-xs text-default-400">
{t("If a service also has its own limit, the smaller number wins")}
</p>
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleSave}
isLoading={saving}
size="sm"
>
{t("Save")}
</Button>
</div>
</div>
);
};
export default FreeQuotaSettings;
@@ -65,6 +65,27 @@ export const PricingTab: React.FC<PricingTabProps> = ({
</SelectItem> </SelectItem>
</Select> </Select>
{formData.charge_type === ChargeType.Free && (
<Input
type="number"
min="0"
step="1"
value={
formData.free_daily_limit === null ||
formData.free_daily_limit === undefined
? ""
: String(formData.free_daily_limit)
}
onChange={(e) => onInputChange("free_daily_limit", e.target.value)}
description={t(
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited"
)}
placeholder={t("e.g. 50")}
label={t("Daily call limit per user")}
labelPlacement="outside"
/>
)}
{formData.charge_type !== ChargeType.Free && ( {formData.charge_type !== ChargeType.Free && (
<> <>
{formData.charge_type === ChargeType.PerToken ? ( {formData.charge_type === ChargeType.PerToken ? (
+1
View File
@@ -68,6 +68,7 @@ export interface MCPServiceFormData {
price?: string; price?: string;
input_token_price?: string; // Price for input tokens when charge_type is PerToken input_token_price?: string; // Price for input tokens when charge_type is PerToken
output_token_price?: string; // Price for output tokens when charge_type is PerToken output_token_price?: string; // Price for output tokens when charge_type is PerToken
free_daily_limit?: string | number | null; // free services only: max calls per user per day (Beijing time), blank/0 = unlimited
enabled: EnabledEnum; // API requires string type enabled: EnabledEnum; // API requires string type
apis: MCPServiceAPIItem[]; apis: MCPServiceAPIItem[];
headers?: MCPAuthHeaderItem[]; // add headers to form data headers?: MCPAuthHeaderItem[]; // add headers to form data
+3 -3
View File
@@ -15,8 +15,8 @@ const getUrlLanguage = (): string | null => {
const getBrowserDefaultLanguage = (): string => { const getBrowserDefaultLanguage = (): string => {
if (typeof window === "undefined") { if (typeof window === "undefined") {
// SSR时根据Accept-Language header判断,这里先返回英语 // SSR时根据Accept-Language header判断,这里先返回中文
return "en"; return "zh-CN";
} }
const browserLang = navigator.language; const browserLang = navigator.language;
@@ -31,7 +31,7 @@ const getBrowserDefaultLanguage = (): string => {
const langPrefix = browserLang.split("-")[0]; const langPrefix = browserLang.split("-")[0];
const match = supportedLanguages.find((lang) => lang.startsWith(langPrefix)); const match = supportedLanguages.find((lang) => lang.startsWith(langPrefix));
return match || "en"; // 最后才fallback到英语 return match || "zh-CN"; // 最后才fallback到中文
}; };
export const getDefaultLanguage = (): string => { export const getDefaultLanguage = (): string => {
+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"
@@ -44,4 +44,7 @@ KEY_SOCIAL_ACCOUNT_X_URL = "social_account_x_url"
KEY_CNAME_A_IP = "cname_a_ip" KEY_CNAME_A_IP = "cname_a_ip"
KEY_DEFAULT_RESOURCE_GROUP = "default_resource_group" KEY_DEFAULT_RESOURCE_GROUP = "default_resource_group"
# Platform-wide daily cap on free MCP services (see api_service billing_service)
KEY_FREE_GLOBAL_DAILY_LIMIT = "free_global_daily_limit"
@@ -21,9 +21,14 @@ class BillingMessageConsumer:
def __init__(self): def __init__(self):
self.queue_name = os.getenv("BILLING_QUEUE_NAME") or "billing.api.calls" self.queue_name = os.getenv("BILLING_QUEUE_NAME") or "billing.api.calls"
self.retry_queue_name = f"{self.queue_name}.retry"
self.connection = None self.connection = None
self.channel = None self.channel = None
self.consuming = False self.consuming = False
# retry times from env, default 3
self.max_retries = int(os.getenv("BILLING_RETRY_TIMES", "3"))
self.retry_base_delay_seconds = int(os.getenv("BILLING_RETRY_BASE_DELAY_SECONDS", "30"))
self.retry_max_delay_seconds = int(os.getenv("BILLING_RETRY_MAX_DELAY_SECONDS", "1800"))
self._setup_connection() self._setup_connection()
def _setup_connection(self): def _setup_connection(self):
@@ -48,6 +53,14 @@ class BillingMessageConsumer:
# Declare queue # Declare queue
self.channel.queue_declare(queue=self.queue_name, durable=True) self.channel.queue_declare(queue=self.queue_name, durable=True)
self.channel.queue_declare(
queue=self.retry_queue_name,
durable=True,
arguments={
"x-dead-letter-exchange": "",
"x-dead-letter-routing-key": self.queue_name,
},
)
# Set QoS, only process one message at a time # Set QoS, only process one message at a time
self.channel.basic_qos(prefetch_count=1) self.channel.basic_qos(prefetch_count=1)
@@ -124,6 +137,29 @@ class BillingMessageConsumer:
body: Message body body: Message body
""" """
message_data = None message_data = None
settled = False
raw_body = body.decode("utf-8", errors="replace") if isinstance(body, (bytes, bytearray)) else str(body)
def _safe_ack() -> None:
nonlocal settled
if settled:
return
settled = True
try:
channel.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
logger.error("Failed to ack message", exc_info=True)
def _safe_nack(requeue: bool) -> None:
nonlocal settled
if settled:
return
settled = True
try:
channel.basic_nack(delivery_tag=method.delivery_tag, requeue=requeue)
except Exception:
logger.error("Failed to nack message", exc_info=True)
try: try:
# Parse message # Parse message
message_data = json.loads(body) message_data = json.loads(body)
@@ -137,27 +173,68 @@ class BillingMessageConsumer:
if success: if success:
# Ack if processed successfully # Ack if processed successfully
channel.basic_ack(delivery_tag=method.delivery_tag) _safe_ack()
logger.info(f"Message processed and acknowledged: {message_data.get('user_id')}") logger.info(f"Message processed and acknowledged: {message_data.get('user_id')}")
else: else:
# If failed, ack and drop (avoid infinite retry) # Retry with limited attempts
channel.basic_ack(delivery_tag=method.delivery_tag) retry_count = self._get_retry_count(properties)
logger.error(f"Message processing failed, message dropped: {message_data.get('user_id')}") if retry_count < self.max_retries:
try:
delay_ms = self._get_retry_delay_ms(retry_count)
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms)
_safe_ack()
logger.warning(
f"Message processing failed, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}, tool={message_data.get('tool_name')}, "
)
except Exception as pub_err:
logger.error(f"Failed to requeue message for retry: {pub_err}", exc_info=True)
# Requeue original message
_safe_nack(requeue=True)
else:
# Exceeded retries; ack and drop
_safe_ack()
self._log_retry_exceeded(
reason="handler_returned_false",
retry_count=retry_count,
properties=properties,
method=method,
message_data=message_data,
raw_body=raw_body,
)
finally: finally:
db.close() try:
db.close()
except Exception:
logger.error("Failed to close db session", exc_info=True)
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logger.error(f"Message format error: {str(e)}, body: {body}") logger.error(f"Message format error: {str(e)}, body: {body}")
# Format error, ack directly (do not requeue) # Format error, ack directly (do not requeue)
channel.basic_ack(delivery_tag=method.delivery_tag) _safe_ack()
except Exception as e: except Exception as e:
logger.error(f"Exception occurred while processing message: {str(e)}", exc_info=True) logger.error(f"Exception occurred while processing message: {str(e)}", exc_info=True)
# On exception, ack and drop (avoid infinite retry)
try: try:
channel.basic_ack(delivery_tag=method.delivery_tag) retry_count = self._get_retry_count(properties)
logger.error(f"Exception handling failed, message dropped") if message_data is not None and retry_count < self.max_retries:
delay_ms = self._get_retry_delay_ms(retry_count)
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms, error=str(e))
_safe_ack()
logger.warning(
f"Exception during processing, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}"
)
else:
_safe_ack()
self._log_retry_exceeded(
reason="exception",
retry_count=retry_count,
properties=properties,
method=method,
message_data=message_data,
error=str(e),
raw_body=raw_body,
)
except: except:
# If even ack fails, log error but do not raise # If even ack fails, log error but do not raise
logger.error("Unable to acknowledge message", exc_info=True) logger.error("Unable to acknowledge message", exc_info=True)
@@ -173,9 +250,62 @@ class BillingMessageConsumer:
int: Retry count int: Retry count
""" """
if properties and properties.headers: if properties and properties.headers:
return properties.headers.get("x-retry-count", 0) raw = properties.headers.get("x-retry-count", 0)
try:
return int(raw)
except Exception:
return 0
return 0 return 0
def _get_retry_delay_ms(self, current_retry_count: int) -> int:
delay_seconds = self.retry_base_delay_seconds * (2 ** current_retry_count)
if delay_seconds > self.retry_max_delay_seconds:
delay_seconds = self.retry_max_delay_seconds
return int(delay_seconds * 1000)
def _publish_delayed_retry(self, channel, message_data: dict, next_retry_count: int, delay_ms: int, error: str | None = None) -> None:
headers: dict[str, str | int] = {"x-retry-count": next_retry_count}
if error:
headers["x-last-error"] = error
props = pika.BasicProperties(
delivery_mode=2,
headers=headers,
expiration=str(delay_ms),
)
channel.basic_publish(exchange="", routing_key=self.retry_queue_name, body=json.dumps(message_data), properties=props)
def _log_retry_exceeded(self, reason: str, retry_count: int, properties, method, message_data: dict | None, error: str | None = None, raw_body: str | None = None) -> None:
headers = {}
if properties and properties.headers:
try:
headers = dict(properties.headers)
except Exception:
headers = {}
event = {
"reason": reason,
"retry_count": retry_count,
"max_retries": self.max_retries,
"queue": self.queue_name,
"routing_key": getattr(method, "routing_key", None),
"delivery_tag": getattr(method, "delivery_tag", None),
"headers": headers,
"user_id": (message_data or {}).get("user_id"),
"service_id": (message_data or {}).get("service_id"),
"api_id": (message_data or {}).get("api_id"),
"tool_name": (message_data or {}).get("tool_name"),
"call_start_time": (message_data or {}).get("call_start_time"),
"call_end_time": (message_data or {}).get("call_end_time"),
}
if error:
event["error"] = error
if raw_body is not None:
event["raw_body"] = raw_body
try:
payload = json.dumps(event, ensure_ascii=False, default=str)
except Exception:
payload = repr(event)
logger.error(f"ALERT billing message retries exceeded: {payload}")
def start_billing_consumer(): def start_billing_consumer():
"""Start billing message consumer""" """Start billing message consumer"""
@@ -0,0 +1,144 @@
"""User submission controller for MCP/Skill/Plugin marketplace."""
import logging
import os
import uuid
from fastapi import APIRouter, Depends, Request, UploadFile, File, Form, HTTPException
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.common.utils.validation_utils import ValidationUtils
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
from services.common.models.submission import Submission
logger = logging.getLogger(__name__)
router = APIRouter()
UPLOAD_DIR = "/tmp/submission_uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@router.post("/submission", summary="User submit MCP/Skill/Plugin")
async def create_submission(
request: Request,
type: str = Form(...),
name: str = Form(...),
description: str = Form(None),
file: UploadFile = File(None),
db: Session = Depends(get_db),
):
try:
user_id = UserUtils.get_request_user_id(request)
except Exception:
user_id = None
if not user_id:
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
if type not in ("mcp", "skill", "plugin"):
return ResponseUtils.error(error_msg=error_msg.INVALID_PARAMETER)
file_path = None
if file:
safe_name = f"{uuid.uuid4()}_{file.filename}"
file_path = os.path.join(UPLOAD_DIR, safe_name)
with open(file_path, "wb") as f:
f.write(await file.read())
sub = Submission(
user_id=user_id,
type=type,
name=name,
description=description,
file_path=file_path,
status="pending",
)
db.add(sub)
db.commit()
db.refresh(sub)
return ResponseUtils.success(data={"id": sub.id, "status": sub.status})
@router.get("/submissions", summary="List user submissions")
def list_submissions(request: Request, db: Session = Depends(get_db)):
try:
user_id = UserUtils.get_request_user_id(request)
except Exception:
user_id = None
if not user_id:
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
subs = db.query(Submission).filter(Submission.user_id == user_id).order_by(Submission.created_at.desc()).all()
data = [
{
"id": s.id,
"type": s.type,
"name": s.name,
"description": s.description,
"status": s.status,
"created_at": s.created_at.isoformat() if s.created_at else None,
"review_note": s.review_note,
}
for s in subs
]
return ResponseUtils.success(data=data)
# Admin endpoints
@router.get("/admin/submissions", summary="Admin list submissions")
def admin_list_submissions(request: Request, status: str = None, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
q = db.query(Submission)
if status:
q = q.filter(Submission.status == status)
subs = q.order_by(Submission.created_at.desc()).all()
data = [
{
"id": s.id,
"user_id": s.user_id,
"type": s.type,
"name": s.name,
"description": s.description,
"file_path": s.file_path,
"status": s.status,
"created_at": s.created_at.isoformat() if s.created_at else None,
"review_note": s.review_note,
}
for s in subs
]
return ResponseUtils.success(data=data)
@router.put("/admin/submission/{sub_id}/approve", summary="Approve submission")
def approve_submission(sub_id: str, request: Request, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
sub = db.query(Submission).filter(Submission.id == sub_id).first()
if not sub:
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
sub.status = "approved"
sub.reviewer_id = UserUtils.get_request_user_id(request)
from datetime import datetime
sub.reviewed_at = datetime.utcnow()
db.commit()
return ResponseUtils.success()
@router.put("/admin/submission/{sub_id}/reject", summary="Reject submission")
def reject_submission(sub_id: str, request: Request, body: dict = None, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
sub = db.query(Submission).filter(Submission.id == sub_id).first()
if not sub:
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
sub.status = "rejected"
sub.reviewer_id = UserUtils.get_request_user_id(request)
from datetime import datetime
sub.reviewed_at = datetime.utcnow()
if body:
sub.review_note = body.get("note")
db.commit()
return ResponseUtils.success()
@@ -34,6 +34,58 @@ def get_sysconfig_service(db: Session = Depends(get_db)) -> SysConfigService:
def get_user_service(db: Session = Depends(get_db)) -> UserService: def get_user_service(db: Session = Depends(get_db)) -> UserService:
return UserService(db) return UserService(db)
@router.get("/free-quota")
def get_free_quota(
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
"""Platform-wide daily cap applied to every free service (None = unlimited)."""
raw = sysconfig_service.get_value_by_key(sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
limit = None
if raw not in (None, ""):
try:
limit = int(raw)
except (TypeError, ValueError):
limit = None
if limit is not None and limit <= 0:
limit = None
return ResponseUtils.success(data={"free_global_daily_limit": limit})
@router.put("/free-quota")
def set_free_quota(
request: Request,
body: dict = Body(...),
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
"""Update the platform-wide daily cap. Empty value clears it (unlimited)."""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
raw = body.get("free_global_daily_limit")
if raw in (None, ""):
sysconfig_service.set_value_by_key(
sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT, "", "Global free daily limit"
)
return ResponseUtils.success(data={"free_global_daily_limit": None})
try:
limit = int(raw)
except (TypeError, ValueError):
return ResponseUtils.error(
message="free_global_daily_limit must be a non-negative integer", code=400
)
if limit < 0:
return ResponseUtils.error(
message="free_global_daily_limit must be a non-negative integer", code=400
)
sysconfig_service.set_value_by_key(
sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT, str(limit), "Global free daily limit"
)
return ResponseUtils.success(data={"free_global_daily_limit": limit})
@router.get("/homepage") @router.get("/homepage")
def get_homepage( def get_homepage(
sysconfig_service: SysConfigService = Depends(get_sysconfig_service), sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
+2
View File
@@ -26,6 +26,7 @@ from services.admin_service.controllers import email_test
from services.admin_service.controllers import upload from services.admin_service.controllers import upload
from services.admin_service.controllers import user_task from services.admin_service.controllers import user_task
from services.admin_service.controllers import resource_group from services.admin_service.controllers import resource_group
from services.admin_service.controllers import submission
@@ -170,6 +171,7 @@ app.include_router(email_test.router, prefix="/api/email_test")
app.include_router(upload.router, prefix="/api/upload") app.include_router(upload.router, prefix="/api/upload")
app.include_router(user_task.router, prefix="/api/onboarding") app.include_router(user_task.router, prefix="/api/onboarding")
app.include_router(resource_group.router, prefix="/api/resource_group") app.include_router(resource_group.router, prefix="/api/resource_group")
app.include_router(submission.router, prefix="/api/submission")
@@ -11,17 +11,20 @@ class McpCallLogRepository:
def __init__(self, db: Session): def __init__(self, db: Session):
self.db = db self.db = db
def create(self, call_log: McpCallLog) -> McpCallLog: def create(self, call_log: McpCallLog, commit: bool = True) -> McpCallLog:
self.db.add(call_log) self.db.add(call_log)
self.db.commit() if commit:
self.db.refresh(call_log) self.db.commit()
self.db.refresh(call_log)
else:
self.db.flush()
return call_log return call_log
def get_by_id(self, log_id: str) -> Optional[McpCallLog]: def get_by_id(self, log_id: str) -> Optional[McpCallLog]:
return self.db.query(McpCallLog).filter(McpCallLog.id == log_id).first() return self.db.query(McpCallLog).filter(McpCallLog.id == log_id).first()
def update_status( def update_status(
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None, commit: bool = True
) -> bool: ) -> bool:
log_record = self.get_by_id(log_id) log_record = self.get_by_id(log_id)
if log_record: if log_record:
@@ -30,7 +33,10 @@ class McpCallLogRepository:
log_record.error_msg = error_msg log_record.error_msg = error_msg
if wallet_history_id: if wallet_history_id:
log_record.wallet_history_id = wallet_history_id log_record.wallet_history_id = wallet_history_id
self.db.commit() if commit:
self.db.commit()
else:
self.db.flush()
return True return True
return False return False
@@ -1,7 +1,7 @@
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from typing import Optional, List, Tuple, cast from typing import Optional, List, Tuple, cast
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import func, literal_column from sqlalchemy import Boolean, func, literal_column
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.dialects.mysql import insert as mysql_insert from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy.sql.schema import Table from sqlalchemy.sql.schema import Table
@@ -91,14 +91,12 @@ class StatsMcpServiceDateRepository:
total = query.scalar() total = query.scalar()
return int(total or 0) return int(total or 0)
def increment(self, service_id: str, stats_date: datetime, inc: int = 1) -> None: def increment(self, service_id: str, stats_date: datetime, inc: int = 1, commit: bool = True):
"""Atomically increment call_count for (service_id, stats_date) under high concurrency. """Atomically increment call_count for (service_id, stats_date) under high concurrency.
Implementation uses MySQL's ON DUPLICATE KEY UPDATE to avoid race conditions. Implementation uses MySQL's ON DUPLICATE KEY UPDATE to avoid race conditions.
If the row does not exist, it will be inserted with call_count=inc; otherwise, call_count is increased. If the row does not exist, it will be inserted with call_count=inc; otherwise, call_count is increased.
""" """
# Logger for debug purposes
print(f"Incrementing call count for service {service_id} on {stats_date} by {inc}")
# Cast mapped table for typing compatibility with mysql_insert # Cast mapped table for typing compatibility with mysql_insert
table: Table = cast(Table, StatsMcpServiceDate.__table__) table: Table = cast(Table, StatsMcpServiceDate.__table__)
@@ -114,7 +112,10 @@ class StatsMcpServiceDateRepository:
updated_at=func.current_timestamp(), updated_at=func.current_timestamp(),
) )
self.db.execute(ondup) self.db.execute(ondup)
self.db.commit() if commit:
self.db.commit()
else:
self.db.flush()
def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None: def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
"""Fallback increment strategy without dialect-specific upsert. """Fallback increment strategy without dialect-specific upsert.
@@ -296,7 +296,7 @@ class UserWalletHistoryRepository:
) )
return total, history return total, history
def add_consume_record(self, wallet_history: UserWalletHistory) -> Optional[UserWalletHistory]: def add_consume_record(self, wallet_history: UserWalletHistory, commit: bool = True) -> Optional[UserWalletHistory]:
""" """
Add consume record to database. Add consume record to database.
@@ -310,12 +310,18 @@ class UserWalletHistoryRepository:
# Add the wallet history record to database # Add the wallet history record to database
# The created_at and updated_at fields should be set by the caller # The created_at and updated_at fields should be set by the caller
self.db.add(wallet_history) self.db.add(wallet_history)
self.db.commit() if commit:
self.db.refresh(wallet_history) self.db.commit()
self.db.refresh(wallet_history)
else:
self.db.flush()
return wallet_history return wallet_history
except Exception as e: except Exception as e:
self.logging.error(f"Failed to add consume record: {str(e)}") self.logging.error(f"Failed to add consume record: {str(e)}")
self.db.rollback() try:
self.db.rollback()
except Exception:
pass
return None return None
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]: def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
@@ -435,4 +441,4 @@ class UserWalletHistoryRepository:
result.append({"stats_day": current_day, "count": amounts_by_day.get(current_day, 0.0)}) result.append({"stats_day": current_day, "count": amounts_by_day.get(current_day, 0.0)})
current_day += timedelta(days=1) current_day += timedelta(days=1)
return result return result
@@ -32,13 +32,16 @@ class UserWalletRepository:
"""Get user wallet with row-level lock for update""" """Get user wallet with row-level lock for update"""
return self.db.query(UserWallet).filter(UserWallet.user_id == user_id).with_for_update().first() return self.db.query(UserWallet).filter(UserWallet.user_id == user_id).with_for_update().first()
def update_balance(self, user_id: str, new_balance: float) -> bool: def update_balance(self, user_id: str, new_balance: float, commit: bool = True) -> bool:
"""Update user balance""" """Update user balance"""
wallet = self.get_by_user_id(user_id) wallet = self.get_by_user_id(user_id)
if wallet: if wallet:
wallet.balance = new_balance wallet.balance = new_balance
wallet.updated_at = datetime.now(timezone.utc) wallet.updated_at = datetime.now(timezone.utc)
self.db.commit() if commit:
self.db.commit()
else:
self.db.flush()
return True return True
return False return False
@@ -3,6 +3,7 @@ Billing message handler service
""" """
import json import json
from re import finditer
import uuid import uuid
import logging import logging
from datetime import datetime, timezone from datetime import datetime, timezone
@@ -44,41 +45,60 @@ class BillingMessageHandler:
Returns: Returns:
bool: Whether processing was successful bool: Whether processing was successful
""" """
call_log_id: Optional[str] = None
tx = self.db.begin()
try: try:
# Parse message
billing_message = self._parse_message(message_data) billing_message = self._parse_message(message_data)
if not billing_message: if not billing_message:
tx.rollback()
return False return False
# Create API call log
call_log_id = self._create_call_log(billing_message) call_log_id = self._create_call_log(billing_message)
if not call_log_id: if not call_log_id:
tx.rollback()
return False return False
# Update stats_mcp_service_date record
# Bucket stats by hour (UTC), aligned to full hours
start_dt = billing_message.call_start_time start_dt = billing_message.call_start_time
start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc) start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc)
# Store as naive UTC datetime to match MySQL DATETIME behavior stats_date = start_utc.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) self.stats_repo.increment(billing_message.service_id, stats_date, 1, commit=False)
self.stats_repo.increment(billing_message.service_id, stats_date, 1)
# Process billing logic
if billing_message.call_success and billing_message.unit_price > 0: if billing_message.call_success and billing_message.unit_price > 0:
success = self._process_billing(billing_message, call_log_id) success = self._process_billing(billing_message, call_log_id, commit=False)
if not success: if not success:
# Update record status to failed self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed", commit=False)
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed") tx.rollback()
return False return False
# Update record status to processed
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED)
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
return True
if not self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, commit=False):
tx.rollback()
return False
tx.commit()
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
# Update wallet cache after successful commit
try:
wallet = self.wallet_repo.get_by_user_id(billing_message.user_id)
if wallet:
self._update_wallet_cache(billing_message.user_id, Decimal(str(wallet.balance)))
except Exception:
pass
return True
except Exception as e: except Exception as e:
logger.error(f"Failed to process billing message: {str(e)}", exc_info=True) logger.error(f"Failed to process billing message: {str(e)}", exc_info=True)
try:
tx.rollback()
except Exception:
pass
if call_log_id:
try:
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing exception")
except Exception:
logger.exception("Failed to update call log status after exception")
return False return False
finally:
if tx.is_active:
tx.rollback()
def _parse_message(self, message_data: dict) -> Optional[BillingMessage]: def _parse_message(self, message_data: dict) -> Optional[BillingMessage]:
""" """
@@ -146,14 +166,14 @@ class BillingMessageHandler:
updated_at=datetime.now(timezone.utc), updated_at=datetime.now(timezone.utc),
) )
created_log = self.call_log_repo.create(call_log) created_log = self.call_log_repo.create(call_log, commit=False)
return created_log.id return created_log.id
except Exception as e: except Exception as e:
logger.error(f"Failed to create API call log record: {str(e)}") logger.error(f"Failed to create API call log record: {str(e)}")
return None return None
def _process_billing(self, billing_message: BillingMessage, call_log_id: str) -> bool: def _process_billing(self, billing_message: BillingMessage, call_log_id: str, commit: bool = True) -> bool:
""" """
Process actual billing logic Process actual billing logic
@@ -183,7 +203,7 @@ class BillingMessageHandler:
# Execute deduction # Execute deduction
new_balance = current_balance - amount new_balance = current_balance - amount
success = self.wallet_repo.update_balance(user_id, float(new_balance)) success = self.wallet_repo.update_balance(user_id, float(new_balance), commit=commit)
if not success: if not success:
logger.error(f"Failed to update user wallet balance - User ID: {user_id}") logger.error(f"Failed to update user wallet balance - User ID: {user_id}")
return False return False
@@ -206,22 +226,15 @@ class BillingMessageHandler:
updated_at=now, updated_at=now,
) )
created_history = self.wallet_history_repo.add_consume_record(wallet_history) created_history = self.wallet_history_repo.add_consume_record(wallet_history, commit=commit)
if not created_history: if not created_history:
logger.error(f"Failed to create wallet history record - User ID: {user_id}") logger.error(f"Failed to create wallet history record - User ID: {user_id}")
# Rollback balance update
self.wallet_repo.update_balance(user_id, float(current_balance))
return False return False
logger.info(f"Wallet history record created successfully - User ID: {user_id}, History ID: {history_id}, Created at: {now}") logger.info(f"Wallet history record created successfully - User ID: {user_id}, History ID: {history_id}, Created at: {now}")
# Update API call record's actual deduction amount and associated history record ID # Update API call record's actual deduction amount and associated history record ID
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id) self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id, commit=commit)
# Update wallet balance cache in Redis
self._update_wallet_cache(user_id, new_balance)
logger.info( logger.info(
f"Billing processing completed successfully - User ID: {user_id}, Amount deducted: {amount}, Balance: {current_balance} -> {new_balance}" f"Billing processing completed successfully - User ID: {user_id}, Amount deducted: {amount}, Balance: {current_balance} -> {new_balance}"
@@ -228,6 +228,21 @@ class McpManagerService:
if body["output_token_price"] != existing_service.output_token_price: if body["output_token_price"] != existing_service.output_token_price:
self._delete_service_price_cache(service_id) self._delete_service_price_cache(service_id)
existing_service.output_token_price = body["output_token_price"] existing_service.output_token_price = body["output_token_price"]
if "free_daily_limit" in body:
raw_limit = body["free_daily_limit"]
if raw_limit in (None, ""):
new_limit = None
else:
try:
new_limit = int(raw_limit)
except (TypeError, ValueError):
raise ValueError("free_daily_limit must be a non-negative integer")
if new_limit < 0:
raise ValueError("free_daily_limit must be a non-negative integer")
if new_limit != existing_service.free_daily_limit:
# api_service reads this out of the 1h price cache
self._delete_service_price_cache(service_id)
existing_service.free_daily_limit = new_limit
if "tags" in body and body["tags"] is not None: if "tags" in body and body["tags"] is not None:
# If provided as array, convert to string for storage # If provided as array, convert to string for storage
if isinstance(body["tags"], list): if isinstance(body["tags"], list):
@@ -316,6 +331,7 @@ class McpManagerService:
"input_token_price": str(float(service.input_token_price)) if service.input_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00", "input_token_price": str(float(service.input_token_price)) if service.input_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00",
"output_token_price": str(float(service.output_token_price)) if service.output_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00", "output_token_price": str(float(service.output_token_price)) if service.output_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00",
"enabled": service.enabled, "enabled": service.enabled,
"free_daily_limit": service.free_daily_limit,
"tags": parse_tags_to_array(service.tags), "tags": parse_tags_to_array(service.tags),
"apis": [{"id": api.id, "name": api.name, "description": api.description,"url":api.path} for api in apis], "apis": [{"id": api.id, "name": api.name, "description": api.description,"url":api.path} for api in apis],
} }
+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)
@@ -37,7 +37,7 @@ class UserApiKeyRepository:
return cached_model return cached_model
# Query from database if not in cache # Query from database if not in cache
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey).first() user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey, UserApiKey.is_deleted == 0).first()
if user_apikey: if user_apikey:
# Cache the result using new SQLAlchemy-specific method # Cache the result using new SQLAlchemy-specific method
CacheUtils.set_sqlalchemy_cache(cache_key, user_apikey, 300) CacheUtils.set_sqlalchemy_cache(cache_key, user_apikey, 300)
@@ -12,13 +12,16 @@ from typing import Tuple, Optional
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from services.common.redis import redis_client from services.common.redis import redis_client
from services.common.redis_keys import RedisKeys
from services.common.rabbitmq import rabbitmq_client from services.common.rabbitmq import rabbitmq_client
from services.common.models.billing import BillingMessage, PreDeductResult, ApiCallLogInfo from services.common.models.billing import BillingMessage, PreDeductResult, ApiCallLogInfo
from services.common.models.mcp_service import ChargeType from services.common.models.mcp_service import ChargeType
from services.common.models.sys_config import SysConfig
from services.common.models.user_wallet import UserWallet from services.common.models.user_wallet import UserWallet
from services.common.database import get_db from services.common.database import get_db
from services.api_service.repositories.mcp_service_repository import McpServiceRepository from services.api_service.repositories.mcp_service_repository import McpServiceRepository
from services.api_service.repositories.user_wallet_repository import UserWalletRepository from services.api_service.repositories.user_wallet_repository import UserWalletRepository
from services.admin_service.constants import sys_config_key
from services.common.logging_config import get_logger from services.common.logging_config import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
@@ -51,10 +54,45 @@ class BillingService:
""" """
try: try:
# Get service price information # Get service price information
price, input_token_price, output_token_price, charge_type = await self._get_service_price(service_id) price, input_token_price, output_token_price, charge_type, free_daily_limit = await self._get_service_price(service_id)
service_price = price service_price = price
match charge_type: match charge_type:
case ChargeType.FREE: case ChargeType.FREE:
# Free services are not billed, but may be limited to N calls
# per user per day (Beijing time) to stop one user draining them.
# The platform-wide cap (sys_config) applies to every free service
# and wins whenever it is the smaller of the two, so a service
# with no per-service value still inherits the global one.
limits = [
(value, scope)
for value, scope in (
(free_daily_limit, "service"),
(self._get_global_free_limit(), "platform"),
)
if value and value > 0
]
if limits:
limit, scope = min(limits, key=lambda item: item[0])
allowed, used = self._check_free_quota(user_id, service_id, limit)
if not allowed:
scope_note = " (platform-wide limit)" if scope == "platform" else ""
logger.warning(
f"Free daily limit reached - User ID: {user_id}, Service ID: {service_id}, "
f"Limit: {limit}{scope_note}, Used: {used}"
)
return PreDeductResult(
success=False,
message=(
f"Free daily limit reached: {limit} calls per day{scope_note}. "
f"Used {used} today, please try again after midnight (Beijing time) "
f"or upgrade to a paid plan"
),
service_price=Decimal("0"),
user_balance=Decimal("0"),
input_token_price=input_token_price,
output_token_price=output_token_price,
charge_type=charge_type.value
)
logger.info(f"Free service, skip billing check - User ID: {user_id}, Service ID: {service_id}") logger.info(f"Free service, skip billing check - User ID: {user_id}, Service ID: {service_id}")
return PreDeductResult( return PreDeductResult(
success=True, success=True,
@@ -168,7 +206,76 @@ class BillingService:
logger.error(f"Failed to send billing message: {str(e)}", exc_info=True) logger.error(f"Failed to send billing message: {str(e)}", exc_info=True)
# Consider saving failed messages to local queue for retry # Consider saving failed messages to local queue for retry
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, Decimal, Decimal, ChargeType]: def _check_free_quota(self, user_id: str, service_id: str, limit: int) -> tuple[bool, int]:
"""Count today's calls for a free service and say whether this one is still allowed.
One Redis key per service/user/Beijing-day, so the counter costs a single
INCR and expires by itself after midnight (no cleanup job).
Returns (allowed, used_today). A rejected call is counted too, otherwise a
client could loop past the limit by simply retrying.
"""
from datetime import timedelta
tz_cst = timezone(timedelta(hours=8))
now_cst = datetime.now(tz_cst)
day = now_cst.strftime("%Y%m%d")
key = f"xpack:free:calls:{service_id}:{user_id}:{day}"
try:
used = int(self.redis.incr(key))
if used == 1:
# seconds until the next Beijing midnight, plus a day of slack
next_midnight = (now_cst + timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
ttl = int((next_midnight - now_cst).total_seconds()) + 86400
self.redis.expire(key, ttl)
except Exception as e:
# Redis being down must not lock paying/free users out of their tools
logger.warning(f"Free quota counter unavailable, allowing call - User ID: {user_id}: {e}")
return True, 0
return used <= limit, used
def _get_global_free_limit(self) -> Optional[int]:
"""Platform-wide daily cap on free services, stored in sys_config.
Reads the same cache key the admin service invalidates on save, so a new
value takes effect on the very next call. Unset / empty / unparsable all
mean "no platform cap" and never fail the request.
"""
cache_key = RedisKeys.sys_config_key(sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
raw = None
try:
cached = self.redis.get(cache_key)
if cached is not None:
raw = None if cached == "__NULL__" else str(cached)
else:
db = next(get_db())
try:
row = (
db.query(SysConfig)
.filter(SysConfig.key == sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
.first()
)
raw = row.value if row is not None else None
finally:
db.close()
# Cache the miss too, so an unset key costs no query per call
self.redis.set(cache_key, "__NULL__" if raw in (None, "") else raw, ex=3600)
except Exception as e:
logger.warning(f"Global free limit unavailable, treating as unset: {e}")
return None
if raw in (None, "", "__NULL__"):
return None
try:
value = int(raw)
except (TypeError, ValueError):
return None
return value if value > 0 else None
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, Decimal, Decimal, ChargeType, Optional[int]]:
""" """
Get service price and charge type Get service price and charge type
@@ -184,7 +291,15 @@ class BillingService:
if cached_data: if cached_data:
try: try:
data = json.loads(cached_data) data = json.loads(cached_data)
return Decimal(data["price"]), Decimal(data["input_token_price"]), Decimal(data["output_token_price"]), ChargeType(data["charge_type"]) # free_daily_limit was added later; older cache entries simply lack it
free_daily_limit = data.get("free_daily_limit")
return (
Decimal(data["price"]),
Decimal(data["input_token_price"]),
Decimal(data["output_token_price"]),
ChargeType(data["charge_type"]),
int(free_daily_limit) if free_daily_limit not in (None, "") else None,
)
except (json.JSONDecodeError, KeyError, ValueError): except (json.JSONDecodeError, KeyError, ValueError):
logger.warning(f"Service price cache data anomaly, re-fetch from database - Service ID: {service_id}") logger.warning(f"Service price cache data anomaly, re-fetch from database - Service ID: {service_id}")
@@ -209,17 +324,20 @@ class BillingService:
input_token_price = Decimal(str(service.input_token_price)) input_token_price = Decimal(str(service.input_token_price))
output_token_price = Decimal(str(service.output_token_price)) output_token_price = Decimal(str(service.output_token_price))
charge_type = service.charge_type charge_type = service.charge_type
free_daily_limit = getattr(service, "free_daily_limit", None)
free_daily_limit = int(free_daily_limit) if free_daily_limit else None
# Cache to Redis # Cache to Redis
cache_data = { cache_data = {
"price": str(price), "price": str(price),
"input_token_price": str(input_token_price), "input_token_price": str(input_token_price),
"output_token_price":str(output_token_price), "output_token_price":str(output_token_price),
"charge_type": charge_type.value "charge_type": charge_type.value,
"free_daily_limit": free_daily_limit
} }
self.redis.set(cache_key, json.dumps(cache_data), ex=self.SERVICE_CACHE_EXPIRE) self.redis.set(cache_key, json.dumps(cache_data), ex=self.SERVICE_CACHE_EXPIRE)
return price,input_token_price,output_token_price, charge_type return price,input_token_price,output_token_price, charge_type, free_daily_limit
finally: finally:
db.close() db.close()
@@ -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
+5
View File
@@ -33,6 +33,11 @@ class McpService(Base):
Numeric(10, 2), nullable=True, comment="Output token price (2 decimal places, for per_token charge type)" Numeric(10, 2), nullable=True, comment="Output token price (2 decimal places, for per_token charge type)"
) )
enabled: Mapped[int] = mapped_column(Integer, nullable=True, comment="Service status: 0=disabled, 1=enabled") enabled: Mapped[int] = mapped_column(Integer, nullable=True, comment="Service status: 0=disabled, 1=enabled")
free_daily_limit: Mapped[int] = mapped_column(
Integer,
nullable=True,
comment="Free service: max calls per user per day (Beijing time). NULL or 0 = unlimited",
)
tags: Mapped[str] = mapped_column(String, nullable=True, comment="Tags") tags: Mapped[str] = mapped_column(String, nullable=True, comment="Tags")
service_type: Mapped[str] = mapped_column(String(255), nullable=True, comment="Service type",default="openapi") service_type: Mapped[str] = mapped_column(String(255), nullable=True, comment="Service type",default="openapi")
created_at: Mapped[datetime] = mapped_column( created_at: Mapped[datetime] = mapped_column(
+22
View File
@@ -0,0 +1,22 @@
"""User submission model for MCP/Skill/Plugin marketplace."""
from sqlalchemy import Column, String, Text, DateTime, JSON, Enum
from sqlalchemy.sql import func
from services.common.models.base import Base
import uuid
class Submission(Base):
__tablename__ = "submission"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
user_id = Column(String(36), nullable=False, index=True)
type = Column(Enum("mcp", "skill", "plugin", name="submission_type"), nullable=False)
name = Column(String(255), nullable=False)
description = Column(Text, nullable=True)
file_path = Column(String(512), nullable=True)
metadata_json = Column(JSON, nullable=True)
status = Column(Enum("pending", "approved", "rejected", name="submission_status"), default="pending", nullable=False)
reviewer_id = Column(String(36), nullable=True)
review_note = Column(Text, nullable=True)
created_at = Column(DateTime, server_default=func.now())
reviewed_at = Column(DateTime, nullable=True)
+8 -3
View File
@@ -30,7 +30,7 @@ class RabbitMQClient:
logger.error(f"Failed to establish RabbitMQ connection: {str(e)}") logger.error(f"Failed to establish RabbitMQ connection: {str(e)}")
raise raise
def publish(self, queue: str, message: str, persistent: bool = True): def publish(self, queue: str, message: str, persistent: bool = True, headers: dict | None = None):
"""Publish message to queue""" """Publish message to queue"""
try: try:
# Ensure connection is available # Ensure connection is available
@@ -46,7 +46,9 @@ class RabbitMQClient:
# Publish message # Publish message
properties = None properties = None
if persistent: if persistent:
properties = pika.BasicProperties(delivery_mode=2) properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
elif headers:
properties = pika.BasicProperties(headers=headers)
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties) self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
logger.info(f"Message successfully published to queue: {queue}, message length: {len(message)} bytes") logger.info(f"Message successfully published to queue: {queue}, message length: {len(message)} bytes")
@@ -61,7 +63,10 @@ class RabbitMQClient:
# Retry sending # Retry sending
self.channel.queue_declare(queue=queue, durable=True) self.channel.queue_declare(queue=queue, durable=True)
properties = pika.BasicProperties(delivery_mode=2) if persistent else None if persistent:
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
else:
properties = pika.BasicProperties(headers=headers or None)
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties) self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
logger.info(f"Message retry successful to queue {queue}") logger.info(f"Message retry successful to queue {queue}")
except Exception as retry_error: except Exception as retry_error:
+6 -6
View File
@@ -66,9 +66,9 @@ class HttpUtils:
if parsed.scheme not in ("http", "https"): if parsed.scheme not in ("http", "https"):
raise ValidationException("URL scheme must be http or https") raise ValidationException("URL scheme must be http or https")
host = (parsed.hostname or "").lower() host = (parsed.hostname or "").lower()
private_prefixes = ("127.", "10.", "192.168.") # Allow LAN/private IPs (user request), only block loopback
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."): if host in ("localhost", "127.0.0.1", "::1") or host.startswith("127."):
raise ValidationException("Access to local/private addresses is not allowed") raise ValidationException("Access to loopback addresses is not allowed")
async with aiohttp.ClientSession( async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout), timeout=aiohttp.ClientTimeout(total=timeout),
@@ -193,9 +193,9 @@ class HttpUtils:
if parsed.scheme not in ("http", "https"): if parsed.scheme not in ("http", "https"):
raise ValidationException("URL scheme must be http or https") raise ValidationException("URL scheme must be http or https")
host = (parsed.hostname or "").lower() host = (parsed.hostname or "").lower()
private_prefixes = ("127.", "10.", "192.168.") # Allow LAN/private IPs (user request), only block loopback
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."): if host in ("localhost", "127.0.0.1", "::1") or host.startswith("127."):
raise ValidationException("Access to local/private addresses is not allowed") raise ValidationException("Access to loopback addresses is not allowed")
async with aiohttp.ClientSession( async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout), timeout=aiohttp.ClientTimeout(total=timeout),