diff --git a/scripts/resource/sql/version-1.1.0.sql b/scripts/resource/sql/version-1.1.0.sql index 3aa0850..fc4d2a0 100644 --- a/scripts/resource/sql/version-1.1.0.sql +++ b/scripts/resource/sql/version-1.1.0.sql @@ -48,14 +48,14 @@ CREATE TABLE `onboarding_task` ( UNIQUE INDEX `uk`(`task_id` ASC, `user_id` ASC) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '新手引导任务表' ROW_FORMAT = DYNAMIC; -CREATE TABLE `stats_mcp_service_date` ( - `stats_date` DATETIME NOT NULL COMMENT '统计时间(小时,UTC)', +CREATE TABLE `stats_mcp_service_date` ( + `stats_date` datetime NOT NULL, `service_id` char(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, - `call_count` int NOT NULL DEFAULT 0 COMMENT '调用次数', + `call_count` int NOT NULL DEFAULT '0' COMMENT '调用次数', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, - PRIMARY KEY (`stats_date`, `service_id`) USING BTREE -) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; + UNIQUE KEY `uk_stats` (`stats_date`,`service_id`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC; ALTER TABLE `temp_mcp_service` ADD COLUMN `headers` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '请求头配置,[{\"name\":\"name\",\"value\":\"value\",\"description\":\"description\"}]' AFTER `auth_token`; diff --git a/services/admin_service/repositories/stats_mcp_service_date_repository.py b/services/admin_service/repositories/stats_mcp_service_date_repository.py index ee05a57..ef8157b 100644 --- a/services/admin_service/repositories/stats_mcp_service_date_repository.py +++ b/services/admin_service/repositories/stats_mcp_service_date_repository.py @@ -97,6 +97,9 @@ class StatsMcpServiceDateRepository: 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. """ + # 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 table: Table = cast(Table, StatsMcpServiceDate.__table__) stmt = mysql_insert(table).values( diff --git a/services/admin_service/repositories/user_repository.py b/services/admin_service/repositories/user_repository.py index b5c8fb6..d13b3f9 100644 --- a/services/admin_service/repositories/user_repository.py +++ b/services/admin_service/repositories/user_repository.py @@ -22,6 +22,7 @@ class UserRepository: user = self.get_by_id(user_id) if not user: return None + user.updated_at = datetime.now(timezone.utc) user.password = password self.db.commit() self.db.refresh(user) @@ -32,7 +33,7 @@ class UserRepository: from services.common.models.user import RegisterType name = email.split("@")[0] if "@" in email else email - now = datetime.now() + now = datetime.now(timezone.utc) user = User( id=str(uuid4()), name=name, diff --git a/services/admin_service/services/billing_message_handler.py b/services/admin_service/services/billing_message_handler.py index 52f4573..a901aa8 100644 --- a/services/admin_service/services/billing_message_handler.py +++ b/services/admin_service/services/billing_message_handler.py @@ -54,6 +54,14 @@ class BillingMessageHandler: call_log_id = self._create_call_log(billing_message) if not call_log_id: return False + + # Update stats_mcp_service_date record + # 统计按小时分桶(UTC 时区),对齐到整点 + 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) + # Store as naive UTC datetime to match MySQL DATETIME behavior + stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) + 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: @@ -62,13 +70,7 @@ class BillingMessageHandler: # Update record status to failed self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed") return False - # Update stats_mcp_service_date record - # 统计按小时分桶(UTC 时区),对齐到整点 - 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) - # Store as naive UTC datetime to match MySQL DATETIME behavior - stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None) - self.stats_repo.increment(billing_message.service_id, stats_date, 1) + # 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}") @@ -125,6 +127,7 @@ class BillingMessageHandler: """ try: log_id = str(uuid.uuid4()) + # Calculate actual cost if call is successful call_log = McpCallLog( id=log_id, user_id=billing_message.user_id, @@ -134,7 +137,7 @@ class BillingMessageHandler: input_params=billing_message.input_params, call_success=billing_message.call_success, unit_price=float(billing_message.unit_price), - actual_cost=0.0, # Initially 0, updated later + actual_cost=float(billing_message.unit_price), call_start_time=billing_message.call_start_time, call_end_time=billing_message.call_end_time, process_status=ProcessStatus.PENDING, diff --git a/services/api_service/controllers/mcp.py b/services/api_service/controllers/mcp.py index b494b75..dcffe5b 100644 --- a/services/api_service/controllers/mcp.py +++ b/services/api_service/controllers/mcp.py @@ -7,6 +7,7 @@ from typing import Optional from datetime import datetime, timezone from starlette.requests import Request from starlette.responses import Response +import asyncio from mcp.server.sse import SseServerTransport from services.api_service.services.mcp_server_factory import McpServerFactory from services.common.logging_config import get_logger @@ -110,6 +111,119 @@ class McpController: 'body': response_body, }) + async def handle_sse_connection_asgi(self, request: Request): + """Return an ASGI app that handles MCP SSE connections. + + Starlette's Route will call this with a Request and then treat + the returned callable as an ASGI app. We implement the SSE handshake + and streaming within the returned ASGI callable using scope/receive/send. + """ + + async def asgi(scope, receive, send): + req = Request(scope) + client_ip = req.client.host if req.client else "unknown" + user_agent = req.headers.get("user-agent", "unknown") + service_id = None + + try: + # Extract service_id from URL path (supports both ID and slug_name) + service_id = self._extract_service_id(req) + if not service_id: + response_body = b"Missing service_id parameter or service not found" + await send({ + 'type': 'http.response.start', + 'status': 400, + 'headers': [ + [b'content-type', b'text/plain'], + [b'content-length', str(len(response_body)).encode()], + ], + }) + await send({'type': 'http.response.body', 'body': response_body}) + return + + logger.info(f"Received SSE connection request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...") + + # Extract user ID (for billing) - must provide valid apikey + user_info = self._extract_user_info(req) + if not user_info: + response_body = b"Missing or invalid apikey parameter" + await send({ + 'type': 'http.response.start', + 'status': 401, + 'headers': [ + [b'content-type', b'text/plain'], + [b'content-length', str(len(response_body)).encode()], + ], + }) + await send({'type': 'http.response.body', 'body': response_body}) + return + + user_id, apikey_id = user_info + + # Get apikey for logging (extract first 10 characters for audit) + apikey = req.query_params.get("apikey", "") or req.query_params.get("authkey", "") + apikey_for_log = apikey[:10] if apikey else None + + # Create MCP server instance (pass user_id and apikey_id for billing and logging) + mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id) + + # Register connection to manager + connection_key = connection_manager.register_connection(service_id, user_id, client_ip) + + try: + # Establish SSE connection and run MCP server + async with self.sse.connect_sse(scope, receive, send) as streams: + logger.info(f"SSE connection established - Service ID: {service_id}, User ID: {user_id}, Client: {client_ip}") + + # Configure server initialization options + init_options = mcp_server.create_initialization_options() + init_options.server_name = f"mcp-service-{service_id}" + + logger.info(f"Server name set to: {init_options.server_name}") + logger.info("MCP server startup complete, waiting for client messages... (ASGI endpoint)") + + try: + # Run MCP server + await mcp_server.run(streams[0], streams[1], init_options) + logger.info(f"MCP server run completed - Service ID: {service_id}, User ID: {user_id}") + except asyncio.CancelledError: + # Client disconnected; treat as normal shutdown + logger.info(f"SSE client disconnected - Service ID: {service_id}, User ID: {user_id}") + except Exception as e: + logger.error(f"Error occurred during MCP server operation: {str(e)}", exc_info=True) + finally: + # Unregister connection + connection_manager.unregister_connection(connection_key) + + # End of ASGI callable; no further Response needed + + except ConnectionError as e: + logger.warning(f"Connection error - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}") + response_body = b"Connection error" + await send({ + 'type': 'http.response.start', + 'status': 503, + 'headers': [ + [b'content-type', b'text/plain'], + [b'content-length', str(len(response_body)).encode()], + ], + }) + await send({'type': 'http.response.body', 'body': response_body}) + except Exception as e: + logger.error(f"SSE connection handling failed - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}", exc_info=True) + response_body = f"Internal server error: {str(e)}".encode('utf-8') + await send({ + 'type': 'http.response.start', + 'status': 500, + 'headers': [ + [b'content-type', b'text/plain'], + [b'content-length', str(len(response_body)).encode()], + ], + }) + await send({'type': 'http.response.body', 'body': response_body}) + + return asgi + def _extract_service_id(self, request: Request) -> Optional[str]: """Extract service ID from request path, supporting both ID and slug_name.""" service_identifier = request.path_params.get("service_id") diff --git a/services/api_service/main.py b/services/api_service/main.py index 89b37bb..bebd437 100644 --- a/services/api_service/main.py +++ b/services/api_service/main.py @@ -197,7 +197,7 @@ def mcp_connections_stats(): # Create MCP Streamable HTTP routes # Use Starlette sub-app to handle MCP protocol's underlying SSE connections mcp_routes = [ - Route("/{service_id}", endpoint=mcp.handle_sse_connection, methods=["GET"]), + Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]), Mount("/messages/", app=mcp.get_sse_mount_handler()), ] diff --git a/services/api_service/services/billing_service.py b/services/api_service/services/billing_service.py index 4277394..82cf45c 100644 --- a/services/api_service/services/billing_service.py +++ b/services/api_service/services/billing_service.py @@ -193,9 +193,11 @@ class BillingService: try: service_repo = McpServiceRepository(db) service = service_repo.get_by_id(service_id) - + if not service: raise ValueError(f"Service not found: {service_id}") + logger.info(f"Service price fetched - Service ID: {service_id}, Price: {service.price}, Input Token Price: {service.input_token_price}, Output Token Price: {service.output_token_price}, Charge Type: {service.charge_type}") + if not service.price: service.price = 0 if not service.input_token_price: @@ -204,7 +206,6 @@ class BillingService: service.output_token_price = 0 price = Decimal(str(service.price)) - input_token_price = Decimal(str(service.input_token_price)) output_token_price = Decimal(str(service.output_token_price)) charge_type = service.charge_type diff --git a/services/api_service/services/mcp_server_factory.py b/services/api_service/services/mcp_server_factory.py index 929ad20..9b8671d 100644 --- a/services/api_service/services/mcp_server_factory.py +++ b/services/api_service/services/mcp_server_factory.py @@ -143,7 +143,7 @@ class McpServerFactory: return [types.TextContent(type="text", text=error_msg)] logger.info(f"Pre-deduction successful - User ID: {user_id}, Deduction amount: {pre_deduct_result.service_price}") - + amount = pre_deduct_result.service_price # 2. Execute tool call db = next(get_db()) call_success = False @@ -162,6 +162,7 @@ class McpServerFactory: input_token = estimated_input_tokens # Calculate input token amount (prices are per million tokens) input_token_amount = (Decimal(str(estimated_input_tokens)) / Decimal("1000000")) * pre_deduct_result.input_token_price + amount = input_token_amount try: # Create service instance @@ -195,6 +196,7 @@ class McpServerFactory: output_token = estimated_output_tokens # Calculate output token amount (prices are per million tokens) output_token_amount = (Decimal(str(estimated_output_tokens)) / Decimal("1000000")) * pre_deduct_result.output_token_price + amount += output_token_amount except Exception as e: logger.error(f"Tool call failed - User ID: {user_id}, Tool: {name}: {str(e)}", exc_info=True) @@ -214,7 +216,7 @@ class McpServerFactory: api_id=call_log_id, tool_name=name, input_params=json.dumps(arguments), - unit_price=(input_token_amount + output_token_amount).quantize(Decimal('0.000001')), + unit_price=amount.quantize(Decimal('0.000001')), input_token=Decimal(str(input_token)).quantize(Decimal('0')), output_token=Decimal(str(output_token)).quantize(Decimal('0')), charge_type=pre_deduct_result.charge_type,