Compare commits

...
16 Commits
Author SHA1 Message Date
riverLethe 4d26a793a3 feat/ui:product detail font size 2025-07-25 23:43:11 +08:00
riverLethe ac849ce4dd feat/ui-0.2:page title 2025-07-25 23:36:47 +08:00
Liujian a9a83fa187 Fix the issue where the user list includes the admin account 2025-07-25 23:17:32 +08:00
lidongjie c2738813eb fix: update log level to DEBUG for improved debugging information 2025-07-25 23:16:16 +08:00
lidongjie 883ada7aac feat: update parameter handling in McpManagerService to use JSON serialization for improved data integrity 2025-07-25 23:09:29 +08:00
lidongjie ad574f0162 Merge branch 'main' of https://github.com/xpack-ai/XPack-MCP-Market 2025-07-25 23:03:42 +08:00
lidongjie 5cc9e9910e feat: enhance parameter parsing in HttpRequestBuilder with robust error handling and support for multiple formats 2025-07-25 23:03:35 +08:00
riverLethe d91c1c5369 feat/ui-0.22:set static NEXT_PUBLIC_MCP_URL 2025-07-25 23:02:42 +08:00
lidongjie f108551780 Merge branch 'main' of https://github.com/xpack-ai/XPack-MCP-Market 2025-07-25 22:46:19 +08:00
lidongjie be19df6273 feat: enhance parameter parsing in McpService with robust error handling and logging 2025-07-25 22:41:00 +08:00
riverLethe 2eb02bb70f fix/op:change op rewrite port to 8002 2025-07-25 22:39:54 +08:00
riverLethe b548f2494d feat/ui:add /mcp url rewrite rule 2025-07-25 22:27:20 +08:00
JackLiu e1fdbdd026 Update README-zh-cn.md 2025-07-25 22:18:42 +08:00
JackLiu 7985c6ae2b Update README.md 2025-07-25 22:18:09 +08:00
riverLethe fdfe2c63bc feat/mcp server change mcp name and fix mcp server can not set bearer token bug 2025-07-25 22:03:37 +08:00
lethe 224a209fea fix/ui-0.2:mcp config text error 2025-07-25 15:04:29 +08:00
19 changed files with 341 additions and 116 deletions

No files matched your search

+1 -1
View File
@@ -11,7 +11,7 @@ ADMIN_PORT=8001
# 调试配置
DEBUG=false
LOG_LEVEL=INFO
LOG_LEVEL=DEBUG
# Redis 配置
REDIS_HOST=xpack-redis
+2 -2
View File
@@ -30,7 +30,7 @@
😍 只需一行命令,即可在 10 分钟内部署你的 MCP 服务市场:
```bash
curl -sSO http://xpack.ai/install/quick-start.sh; bash quick-start.sh
curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
```
该脚本会自动安装 MySQL、Redis、RabbitMQ,并启动 XPack 服务。
@@ -83,7 +83,7 @@ XPack-MCP-Market 依赖以下服务(可容器化或自行安装):
### 🟢 快速部署命令
```bash
curl -sSO http://xpack.ai/install/quick-start.sh; bash quick-start.sh
curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
```
支持系统包括:
+2 -2
View File
@@ -29,7 +29,7 @@ Everything is open-source and licensed under **Apache 2.0** — ready for commer
😍 Deploying XPack is incredibly simple. With just one command line, you can deploy your MCP Market in 10 minutes.
```bash
curl -sSO http://xpack.ai/install/quick-start.sh; bash quick-start.sh
curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
```
<br>
@@ -83,7 +83,7 @@ Ensure these services are available and properly configured before running the b
😍 Deploying XPack is incredibly simple. With just one command line, you can deploy your MCP Market in 10 minutes.
```bash
curl -sSO http://xpack.ai/install/quick-start.sh; bash quick-start.sh
curl -sSO https://xpack.ai/install/quick-start.sh; bash quick-start.sh
```
<details>
+2 -5
View File
@@ -1,15 +1,12 @@
import React, { Suspense } from "react";
import { MarketplaceMain } from "@/components/marketplace/Main";
import React from "react";
import type { Metadata } from "next";
import { createBaseMetadata, getDynamicTitle } from "@/shared/utils/metadata";
import { Navigation } from "@/shared/components/Navigation";
import { Footer } from "@/shared/components/Footer";
import { fetchServices } from "@/services/marketplaceService";
import type { ServiceData } from "@/shared/types/marketplace";
import { ThemeSelector } from "@/components/theme/ThemeSelector";
export async function generateMetadata(): Promise<Metadata> {
const title = await getDynamicTitle("Marketplace");
const title = await getDynamicTitle();
return createBaseMetadata(title);
}
@@ -66,9 +66,9 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
onInputChange('auth_token', '');
}}
>
<SelectItem key="none" value="none">{t('None')}</SelectItem>
<SelectItem key="apikey" value="apikey">{t('API Key')}</SelectItem>
<SelectItem key="bearer-token" value="bearer-token">{t('Bearer Token')}</SelectItem>
<SelectItem key="none" value={AuthMethod.None}>{t('None')}</SelectItem>
<SelectItem key="apikey" value={AuthMethod.APIKey}>{t('API Key')}</SelectItem>
<SelectItem key="token" value={AuthMethod.BearerToken}>{t('Bearer Token')}</SelectItem>
</Select>
{/* auth config input fields */}
@@ -27,10 +27,33 @@ export const ProductDetailThemeSelector: React.FC<
const { platformConfig } = usePlatformConfig();
const currentTheme = platformConfig.theme || Theme.DEFAULT;
const [url, setUrl] = useState<string>(process.env.NEXT_PUBLIC_MCP_URL || "");
const [mcpName, setMcpName] = useState<string>("");
useEffect(() => {
if (url || !product.slug_name) return;
setUrl(`${window.location.origin}/mcp/${product.slug_name}`);
setUrl(`${window.location.protocol}//${window.location.hostname}:8002/mcp/${product.slug_name}`);
}, [product.slug_name]);
function sanitizeMCPServerName(rawName: string | undefined): string {
if (!rawName) return "xpack-mcp-service";
// Lower-case & replace invalid chars with hyphen
let name = rawName
.trim()
.toLowerCase()
.replace(/[^a-z0-9-_]+/g, "-");
// Collapse multiple hyphens
name = name.replace(/-+/g, "-");
// Trim leading/trailing hyphens
name = name.replace(/^-+/, "").replace(/-+$/, "");
return name || "mcp-service";
}
useEffect(() => {
setMcpName(sanitizeMCPServerName(platformConfig.name));
}, [platformConfig.name]);
//Render the About page based on the current theme
switch (currentTheme) {
@@ -41,6 +64,7 @@ export const ProductDetailThemeSelector: React.FC<
breadcrumbs={breadcrumbs}
url={url}
navItems={NavigationItems}
mcpName={mcpName}
/>
);
case Theme.CLASSIC:
@@ -50,6 +74,7 @@ export const ProductDetailThemeSelector: React.FC<
breadcrumbs={breadcrumbs}
url={url}
navItems={NavigationItems}
mcpName={mcpName}
/>
);
case Theme.CREATIVE:
@@ -59,6 +84,7 @@ export const ProductDetailThemeSelector: React.FC<
breadcrumbs={breadcrumbs}
url={url}
navItems={NavigationItems}
mcpName={mcpName}
/>
);
case Theme.TEMU:
@@ -68,6 +94,7 @@ export const ProductDetailThemeSelector: React.FC<
breadcrumbs={breadcrumbs}
url={url}
navItems={NavigationItems}
mcpName={mcpName}
/>
);
case Theme.DEFAULT:
@@ -76,7 +103,7 @@ export const ProductDetailThemeSelector: React.FC<
<div className="min-h-screen bg-background flex flex-col justify-between">
<Navigation items={NavigationItems} />
<main className="flex-1">
<ProductDetailClient product={product} />
<ProductDetailClient product={product} mcpName={mcpName} />
</main>
<div>
<Footer />
@@ -21,7 +21,7 @@ interface DetailProps {
name: string;
}[];
url?: string;
mcpName?: string;
// navigation items
navItems?: NavigationItem[];
}
@@ -30,6 +30,7 @@ export const Detail: React.FC<DetailProps> = ({
product,
breadcrumbs,
url,
mcpName,
navItems = [],
}) => {
const { t } = useTranslation();
@@ -83,7 +84,7 @@ export const Detail: React.FC<DetailProps> = ({
))}
</div>
)}
<p className="text-lg mb-4">
<p className="text-sm mb-4">
{product.short_description ||
t("No description available")}
</p>
@@ -93,7 +94,7 @@ export const Detail: React.FC<DetailProps> = ({
{/* Price section */}
{product.charge_type && (
<div className="flex gap-2 items-center">
<b className="text-xs">{t("Price")}:</b>
<b className="text-md">{t("Price")}:</b>
<Price
price={product.price}
charge_type={product.charge_type}
@@ -220,9 +221,9 @@ export const Detail: React.FC<DetailProps> = ({
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"xpack-mcp-market": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"autoApprove"":"all",
"autoApprove":"all",
"url": "${url}?apikey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-API-Key}`}</span>
{`"
@@ -21,7 +21,7 @@ interface DetailProps {
name: string;
}[];
url?: string;
mcpName?: string;
// navigation items
navItems?: NavigationItem[];
}
@@ -30,6 +30,7 @@ export const Detail: React.FC<DetailProps> = ({
product,
breadcrumbs,
url,
mcpName,
navItems = [],
}) => {
const { t } = useTranslation();
@@ -154,9 +155,9 @@ export const Detail: React.FC<DetailProps> = ({
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"xpack-mcp-market": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"autoApprove"":"all",
"autoApprove":"all",
"url": "${url}?apikey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-API-Key}`}</span>
{`"
@@ -21,7 +21,7 @@ interface DetailProps {
name: string;
}[];
url?: string;
mcpName?: string;
// navigation items
navItems?: NavigationItem[];
}
@@ -30,6 +30,7 @@ export const Detail: React.FC<DetailProps> = ({
product,
breadcrumbs,
url,
mcpName,
navItems = [],
}) => {
const { t } = useTranslation();
@@ -148,9 +149,9 @@ export const Detail: React.FC<DetailProps> = ({
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"xpack-mcp-market": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"autoApprove"":"all",
"autoApprove":"all",
"url": "${url}?apikey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-API-Key}`}</span>
{`"
@@ -21,7 +21,7 @@ interface DetailProps {
name: string;
}[];
url?: string;
mcpName?: string;
// navigation items
navItems?: NavigationItem[];
}
@@ -30,6 +30,7 @@ export const Detail: React.FC<DetailProps> = ({
product,
breadcrumbs,
url,
mcpName,
navItems = [],
}) => {
const { t } = useTranslation();
@@ -169,9 +170,9 @@ export const Detail: React.FC<DetailProps> = ({
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"xpack-mcp-market": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"autoApprove"":"all",
"autoApprove":"all",
"url": "${url}?apikey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-API-Key}`}</span>
{`"
@@ -16,11 +16,13 @@ interface ProductDetailClientProps {
link: string;
name: string
}[];
mcpName?: string;
}
export const ProductDetailClient: React.FC<ProductDetailClientProps> = ({
product,
breadcrumbs
breadcrumbs,
mcpName
}) => {
const { t } = useTranslation();
@@ -56,7 +58,7 @@ export const ProductDetailClient: React.FC<ProductDetailClientProps> = ({
</div>
{/* Right side - Server Config */}
<ServerConfig product={product} />
<ServerConfig product={product} mcpName={mcpName} />
</div>
{/* SEO-friendly: Simple text content for search engines */}
@@ -63,7 +63,7 @@ export const ProductHeader: React.FC<ProductHeaderProps> = ({
))}
</div>
)}
<p className="text-lg mb-4">
<p className="text-sm mb-4">
{product.short_description || t("No description available")}
</p>
</div>
@@ -72,7 +72,7 @@ export const ProductHeader: React.FC<ProductHeaderProps> = ({
{/* Price section */}
{product.charge_type && (
<div className="flex gap-2 items-center">
<b className="text-xs">{t("Price")}:</b>
<b className="text-md">{t("Price")}:</b>
<Price price={product.price} charge_type={product.charge_type} />
</div>
)}
@@ -10,9 +10,10 @@ import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext";
interface ServerConfigProps {
product: ServiceData;
mcpName?: string;
}
export const ServerConfig: React.FC<ServerConfigProps> = ({ product }) => {
export const ServerConfig: React.FC<ServerConfigProps> = ({ product, mcpName }) => {
const { t } = useTranslation();
const { handleLogin } = useAuth();
@@ -20,7 +21,7 @@ export const ServerConfig: React.FC<ServerConfigProps> = ({ product }) => {
const [url, setUrl] = useState<string>(process.env.NEXT_PUBLIC_MCP_URL || "");
useEffect(() => {
if (url || !product.slug_name) return
setUrl(`${window.location.origin}/mcp/${product.slug_name}`);
setUrl(`${window.location.protocol}//${window.location.hostname}:8002/mcp/${product.slug_name}`);
}, [product.slug_name]);
@@ -44,9 +45,9 @@ export const ServerConfig: React.FC<ServerConfigProps> = ({ product }) => {
<code className="text-gray-300 whitespace-pre-wrap">
{`{
"mcpServers": {
"xpack-mcp-market": {
"${mcpName || "xpack-mcp-market"}": {
"type": "sse",
"autoApprove"":"all",
"autoApprove":"all",
"url": "${url}?apikey=`}
<span className="text-success">{`{Your-${platformConfig?.name}-API-Key}`}</span>
{`"
+2 -2
View File
@@ -926,8 +926,8 @@ print_xpack_mcp_market_info() {
webPort=$(read_env_var ${ENV_XPACK_MCP_MARKET_WEB_PORT})
echo_pass "XPack MCP Market has run successfully."
echo_info "The Web UI access information is as follows:"
echo_info "Internal network access address: http://$(read_env_var ${ENV_LAN_IP}):${webPort}/admin-signin"
echo_info "External network access address: http://$(read_env_var ${ENV_EXTERNAL_IP}):${webPort}/admin-signin"
echo_info "Internal network access address: http://$(read_env_var ${ENV_LAN_IP}):${webPort}/admin"
echo_info "External network access address: http://$(read_env_var ${ENV_EXTERNAL_IP}):${webPort}/admin"
echo_info "Inital Username: admin"
echo_info "Inital Password: 123456789"
echo ""
+3 -3
View File
@@ -1,11 +1,11 @@
{
"version": "v0.1",
"releaseDate": "2025-07-20",
"version": "v0.2",
"releaseDate": "2025-07-24",
"description": "This is the latest release of the application.",
"downloads": {
"program": {
"xpack_mcp_market": {
"version": "v0.1.0",
"version": "v0.2.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"
@@ -78,9 +78,9 @@ class UserRepository:
return user
def get_user_list(self, offset: int, limit: int) -> tuple[int, list[User]]:
total = self.db.query(User).filter(User.is_deleted == 0 and User.role_id == 2).count()
total = self.db.query(User).filter(User.is_deleted == 0, User.role_id == 2).count()
users = self.db.query(User).filter(User.is_deleted == 0 and User.role_id == 2).offset(offset).limit(limit).all()
users = self.db.query(User).filter(User.is_deleted == 0, User.role_id == 2).offset(offset).limit(limit).all()
return total, users
def get_admin_user(self) -> Optional[User]:
@@ -345,14 +345,14 @@ class McpManagerService:
tool_api.description = api.description or api.summary
tool_api.path = api.path
tool_api.method = HttpMethod(api.method)
tool_api.header_parameters = str(api.header_parameters) if api.header_parameters else ""
tool_api.query_parameters = str(api.query_parameters) if api.query_parameters else ""
tool_api.path_parameters = str(api.path_parameters) if api.path_parameters else ""
tool_api.request_body_schema = str(api.request_body_schema) if api.request_body_schema else ""
tool_api.header_parameters = json.dumps(api.header_parameters) if api.header_parameters else ""
tool_api.query_parameters = json.dumps(api.query_parameters) if api.query_parameters else ""
tool_api.path_parameters = json.dumps(api.path_parameters) if api.path_parameters else ""
tool_api.request_body_schema = json.dumps(api.request_body_schema) if api.request_body_schema else ""
tool_api.response_schema = json.dumps(api.response_schema) if api.response_schema else ""
tool_api.response_examples = str(api.response_examples) if api.response_examples else ""
tool_api.response_headers = str(api.response_headers) if api.response_headers else ""
tool_api.operation_examples = str(api.operation_examples) if api.operation_examples else ""
tool_api.response_examples = json.dumps(api.response_examples) if api.response_examples else ""
tool_api.response_headers = json.dumps(api.response_headers) if api.response_headers else ""
tool_api.operation_examples = json.dumps(api.operation_examples) if api.operation_examples else ""
tool_api.enabled = 1 # Default enabled
tool_api.is_deleted = 0
@@ -418,14 +418,14 @@ class McpManagerService:
temp_api.description = api.description or api.summary or ""
temp_api.path = api.path
temp_api.method = TempHttpMethod(api.method.upper())
temp_api.header_parameters = str(api.header_parameters) if api.header_parameters else ""
temp_api.query_parameters = str(api.query_parameters) if api.query_parameters else ""
temp_api.path_parameters = str(api.path_parameters) if api.path_parameters else ""
temp_api.request_body_schema = str(api.request_body_schema) if api.request_body_schema else ""
temp_api.response_schema = str(api.response_schema) if api.response_schema else ""
temp_api.response_examples = str(api.response_examples) if api.response_examples else ""
temp_api.response_headers = str(api.response_headers) if api.response_headers else ""
temp_api.operation_examples = str(api.operation_examples) if api.operation_examples else ""
temp_api.header_parameters = json.dumps(api.header_parameters) if api.header_parameters else ""
temp_api.query_parameters = json.dumps(api.query_parameters) if api.query_parameters else ""
temp_api.path_parameters = json.dumps(api.path_parameters) if api.path_parameters else ""
temp_api.request_body_schema = json.dumps(api.request_body_schema) if api.request_body_schema else ""
temp_api.response_schema = json.dumps(api.response_schema) if api.response_schema else ""
temp_api.response_examples = json.dumps(api.response_examples) if api.response_examples else ""
temp_api.response_headers = json.dumps(api.response_headers) if api.response_headers else ""
temp_api.operation_examples = json.dumps(api.operation_examples) if api.operation_examples else ""
temp_api.enabled = 0 # Default disabled, requires admin confirmation
temp_api.is_deleted = 0
+144 -33
View File
@@ -1,10 +1,12 @@
import json
import ast
import mcp.types as types
from typing import List, Optional
from services.common.models.mcp_tool_api import McpToolApi
from services.common.models.mcp_service import McpService as McpServiceModel
from services.api_service.repositories.mcp_tool_api_repository import McpToolApiRepository
from services.api_service.repositories.mcp_service_repository import McpServiceRepository
from services.common.logging_config import get_logger
class McpService:
@@ -13,6 +15,75 @@ class McpService:
def __init__(self, tool_api_repository: McpToolApiRepository, service_repository: McpServiceRepository):
self.tool_api_repository = tool_api_repository
self.service_repository = service_repository
self.logger = get_logger(__name__)
def _safe_parse_params(self, params_str: str, tool_name: str, param_type: str) -> list:
"""
Safely parse parameter string that might be in JSON or Python dict format
Args:
params_str: Parameter string to parse
tool_name: Tool name for logging
param_type: Type of parameters (e.g., 'query', 'path', 'body')
Returns:
list: Parsed parameters or empty list if parsing fails
"""
if not params_str or params_str.strip() == '':
return []
# Remove any surrounding whitespace
params_str = params_str.strip()
# Try different parsing strategies
# Strategy 1: Standard JSON format (with double quotes)
try:
result = json.loads(params_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
# If it's a single dict, wrap it in a list
return [result]
else:
self.logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters in {tool_name}")
return []
except json.JSONDecodeError:
pass # Try next strategy
# Strategy 2: Python literal format (with single quotes)
try:
result = ast.literal_eval(params_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
# If it's a single dict, wrap it in a list
return [result]
else:
self.logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters in {tool_name}")
return []
except (ValueError, SyntaxError):
pass # Try next strategy
# Strategy 3: Try to convert Python format to JSON format
try:
# Replace single quotes with double quotes for JSON compatibility
# This is a simple approach that works for most cases
json_str = params_str.replace("'", '"').replace('True', 'true').replace('False', 'false').replace('None', 'null')
result = json.loads(json_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
return [result]
else:
self.logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters in {tool_name}")
return []
except json.JSONDecodeError:
pass # All strategies failed
# If all strategies fail, log the error
self.logger.warning(f"Failed to parse {param_type} parameters for {tool_name}. Data: {params_str[:100]}...")
return []
def get_tools_by_service_id(self, service_id: str) -> List[types.Tool]:
"""
@@ -50,14 +121,21 @@ class McpService:
# Build input schema
input_schema = self._build_input_schema(tool_api)
# If no schema was built (no parameters), create a minimal schema
if input_schema is None:
input_schema = {
"type": "object",
"properties": {},
"required": []
}
return types.Tool(
name=tool_api.name,
title=tool_api.name.replace('_', ' ').title(),
description=tool_api.description,
inputSchema=input_schema
)
except Exception as e:
print(f"Tool conversion failed {tool_api.name}: {e}")
self.logger.error(f"Tool conversion failed for {tool_api.name}: {e}")
return None
def _build_input_schema(self, tool_api: McpToolApi) -> dict:
@@ -78,44 +156,77 @@ class McpService:
# Parse path parameters
if tool_api.path_parameters:
try:
path_params = json.loads(tool_api.path_parameters)
for param in path_params:
if isinstance(param, dict) and "name" in param:
schema["properties"][param["name"]] = {
"type": param.get("type", "string"),
"description": param.get("description", f"Path parameter: {param['name']}")
}
if param.get("required", False):
schema["required"].append(param["name"])
except (json.JSONDecodeError, TypeError):
pass
path_params = self._safe_parse_params(tool_api.path_parameters, tool_api.name, 'path')
for param in path_params:
if isinstance(param, dict) and "name" in param:
# Handle both direct type and schema.type formats
param_type = "string" # default
if "type" in param:
param_type = param["type"]
elif "schema" in param and isinstance(param["schema"], dict) and "type" in param["schema"]:
param_type = param["schema"]["type"]
schema["properties"][param["name"]] = {
"type": param_type,
"description": param.get("description", f"Path parameter: {param['name']}")
}
if param.get("required", False):
schema["required"].append(param["name"])
# Parse query parameters
if tool_api.query_parameters:
try:
query_params = json.loads(tool_api.query_parameters)
for param in query_params:
if isinstance(param, dict) and "name" in param:
schema["properties"][param["name"]] = {
"type": param.get("type", "string"),
"description": param.get("description", f"Query parameter: {param['name']}")
}
if param.get("required", False):
schema["required"].append(param["name"])
except (json.JSONDecodeError, TypeError):
pass
query_params = self._safe_parse_params(tool_api.query_parameters, tool_api.name, 'query')
for param in query_params:
if isinstance(param, dict) and "name" in param:
# Handle both direct type and schema.type formats
param_type = "string" # default
if "type" in param:
param_type = param["type"]
elif "schema" in param and isinstance(param["schema"], dict) and "type" in param["schema"]:
param_type = param["schema"]["type"]
schema["properties"][param["name"]] = {
"type": param_type,
"description": param.get("description", f"Query parameter: {param['name']}")
}
if param.get("required", False):
schema["required"].append(param["name"])
# Parse request body parameters
if tool_api.request_body_schema:
# Try to parse as parameters list first, then as schema object
try:
body_schema = json.loads(tool_api.request_body_schema)
if isinstance(body_schema, dict) and "properties" in body_schema:
schema["properties"].update(body_schema["properties"])
if "required" in body_schema:
schema["required"].extend(body_schema["required"])
except (json.JSONDecodeError, TypeError):
pass
# First try to parse as parameters list (similar to query/path parameters)
body_params = self._safe_parse_params(tool_api.request_body_schema, tool_api.name, 'body')
if body_params:
# If it's a list of parameters, process them like query parameters
for param in body_params:
if isinstance(param, dict) and "name" in param:
param_type = "string" # default
if "type" in param:
param_type = param["type"]
elif "schema" in param and isinstance(param["schema"], dict) and "type" in param["schema"]:
param_type = param["schema"]["type"]
schema["properties"][param["name"]] = {
"type": param_type,
"description": param.get("description", f"Body parameter: {param['name']}")
}
if param.get("required", False):
schema["required"].append(param["name"])
else:
# If not a parameters list, try to parse as JSON schema object
try:
body_schema = json.loads(tool_api.request_body_schema)
if isinstance(body_schema, dict) and "properties" in body_schema:
schema["properties"].update(body_schema["properties"])
if "required" in body_schema:
schema["required"].extend(body_schema["required"])
except json.JSONDecodeError:
pass # Already logged by _safe_parse_params
except Exception as e:
self.logger.warning(f"Failed to parse request body schema for {tool_api.name}: {e}")
return schema
def get_tool_by_name(self, service_id: str, tool_name: str) -> Optional[McpToolApi]:
+107 -24
View File
@@ -2,6 +2,7 @@
HTTP client utility - Build and handle HTTP requests
"""
import json
import ast
from typing import Dict, Any, Optional
from services.common.logging_config import get_logger
@@ -11,6 +12,67 @@ logger = get_logger(__name__)
class HttpRequestBuilder:
"""HTTP request builder"""
def _safe_parse_params(self, params_str: str, param_type: str) -> list:
"""
Safely parse parameter string that might be in JSON or Python dict format
Args:
params_str: Parameter string to parse
param_type: Type of parameters (e.g., 'query', 'path', 'body')
Returns:
list: Parsed parameters or empty list if parsing fails
"""
if not params_str or params_str.strip() == '':
return []
# Remove any surrounding whitespace
params_str = params_str.strip()
# Strategy 1: Standard JSON format (with double quotes)
try:
result = json.loads(params_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
return [result]
else:
logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters")
return []
except json.JSONDecodeError:
pass # Try next strategy
# Strategy 2: Python literal format (with single quotes)
try:
result = ast.literal_eval(params_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
return [result]
else:
logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters")
return []
except (ValueError, SyntaxError):
pass # Try next strategy
# Strategy 3: Try to convert Python format to JSON format
try:
json_str = params_str.replace("'", '"').replace('True', 'true').replace('False', 'false').replace('None', 'null')
result = json.loads(json_str)
if isinstance(result, list):
return result
elif isinstance(result, dict):
return [result]
else:
logger.warning(f"Expected list or dict but got {type(result)} for {param_type} parameters")
return []
except json.JSONDecodeError:
pass # All strategies failed
# If all strategies fail, log the error
logger.warning(f"Failed to parse {param_type} parameters. Data: {params_str[:100]}...")
return []
def build_request(self, tool_config, arguments: dict, auth_info: dict) -> Dict[str, Any]:
"""
Build HTTP request information
@@ -94,11 +156,11 @@ class HttpRequestBuilder:
Returns:
str: URL after parameter replacement
"""
if not tool_config.path_parameters:
if not hasattr(tool_config, 'path_parameters') or not tool_config.path_parameters:
return url
try:
path_params = json.loads(tool_config.path_parameters)
path_params = self._safe_parse_params(tool_config.path_parameters, 'path')
logger.debug(f"Processing path parameters: {path_params}")
for param in path_params:
@@ -110,7 +172,7 @@ class HttpRequestBuilder:
logger.debug(f"URL after path parameter replacement: {url}")
except (json.JSONDecodeError, TypeError) as e:
except Exception as e:
logger.warning(f"Path parameter parsing failed: {e}")
return url
@@ -133,7 +195,7 @@ class HttpRequestBuilder:
self._add_auth_headers(headers, auth_info)
# Add custom headers
self._add_custom_headers(headers, tool_config, arguments)
self._add_custom_headers(tool_config, arguments, headers)
logger.debug(f"Final request headers: {headers}")
return headers
@@ -158,20 +220,20 @@ class HttpRequestBuilder:
token_display = auth_token[:8] + "..." if len(auth_token) > 8 else "***"
logger.debug(f"Added auth header: {auth_header} = {token_display}")
def _add_custom_headers(self, headers: Dict[str, str], tool_config, arguments: dict) -> None:
def _add_custom_headers(self, tool_config, arguments: dict, headers: Dict[str, str]) -> None:
"""
Add custom headers
Args:
headers: Request headers dictionary
tool_config: Tool configuration
arguments: Tool parameters
headers: Request headers dictionary
"""
if not tool_config.header_parameters:
if not hasattr(tool_config, 'custom_headers') or not tool_config.custom_headers:
return
try:
header_params = json.loads(tool_config.header_parameters)
header_params = self._safe_parse_params(tool_config.custom_headers, 'header')
logger.debug(f"Processing custom headers: {header_params}")
for param in header_params:
@@ -181,7 +243,7 @@ class HttpRequestBuilder:
headers[param_name] = str(arguments[param_name])
logger.debug(f"Added custom header: {param_name} = {arguments[param_name]}")
except (json.JSONDecodeError, TypeError) as e:
except Exception as e:
logger.warning(f"Header parameter parsing failed: {e}")
def _build_query_params(self, tool_config, arguments: dict) -> Dict[str, Any]:
@@ -197,11 +259,11 @@ class HttpRequestBuilder:
"""
query_params = {}
if not tool_config.query_parameters:
if not hasattr(tool_config, 'query_parameters') or not tool_config.query_parameters:
return query_params
try:
query_param_defs = json.loads(tool_config.query_parameters)
query_param_defs = self._safe_parse_params(tool_config.query_parameters, 'query')
logger.debug(f"Processing query parameter definitions: {query_param_defs}")
for param in query_param_defs:
@@ -209,10 +271,11 @@ class HttpRequestBuilder:
param_name = param["name"]
if param_name in arguments:
query_params[param_name] = arguments[param_name]
logger.debug(f"Added query parameter: {param_name} = {arguments[param_name]}")
logger.debug(f"Built query parameters: {query_params}")
except (json.JSONDecodeError, TypeError) as e:
except Exception as e:
logger.warning(f"Query parameter parsing failed: {e}")
return query_params
@@ -228,23 +291,43 @@ class HttpRequestBuilder:
Returns:
Optional[Dict[str, Any]]: Request body dictionary, returns None if not needed
"""
if not tool_config.request_body_schema or tool_config.method.value not in ["POST", "PUT", "PATCH"]:
if not hasattr(tool_config, 'request_body_schema') or not tool_config.request_body_schema or tool_config.method.value not in ["POST", "PUT", "PATCH"]:
return None
try:
body_schema = json.loads(tool_config.request_body_schema)
logger.debug(f"Request body schema: {body_schema}")
if isinstance(body_schema, dict) and "properties" in body_schema:
# First try to parse as parameters list (similar to query/path parameters)
body_params = self._safe_parse_params(tool_config.request_body_schema, 'body')
if body_params:
# If it's a list of parameters, process them like query parameters
request_body = {}
for prop_name in body_schema["properties"]:
if prop_name in arguments:
request_body[prop_name] = arguments[prop_name]
for param in body_params:
if isinstance(param, dict) and "name" in param:
param_name = param["name"]
if param_name in arguments:
request_body[param_name] = arguments[param_name]
logger.debug(f"Built request body: {request_body}")
return request_body
except (json.JSONDecodeError, TypeError) as e:
if request_body:
logger.debug(f"Built request body from parameters: {request_body}")
return request_body
else:
# If not a parameters list, try to parse as JSON schema object
try:
body_schema = json.loads(tool_config.request_body_schema)
logger.debug(f"Request body schema: {body_schema}")
if isinstance(body_schema, dict) and "properties" in body_schema:
request_body = {}
for prop_name in body_schema["properties"]:
if prop_name in arguments:
request_body[prop_name] = arguments[prop_name]
if request_body:
logger.debug(f"Built request body from schema: {request_body}")
return request_body
except json.JSONDecodeError:
logger.warning(f"Failed to parse request body schema as JSON")
except Exception as e:
logger.warning(f"Request body schema parsing failed: {e}")
return None