|
29 | 29 |
|
30 | 30 | # Import unified operation system |
31 | 31 | from api_decorators import operation |
32 | | -from mcp import handle_mcp_request |
33 | | -from ollama_service import ChatRequest, ChatResponse, ModelListResponse, OllamaService |
| 32 | +# FastMCP client for direct ticket MCP calls (no AI) |
| 33 | +from fastmcp import Client as MCPClient |
| 34 | + |
| 35 | +# Ticket MCP server URL (same as in agents.py) |
| 36 | +TICKET_MCP_SERVER_URL = "https://yodrrscbpxqnslgugwow.supabase.co/functions/v1/mcp/a7f2b8c4-d3e9-4f1a-b5c6-e8d9f0123456" |
| 37 | +from mcp_handler import handle_mcp_request |
| 38 | +from ollama_service import (ChatRequest, ChatResponse, ModelListResponse, |
| 39 | + OllamaService) |
34 | 40 | from pydantic import ValidationError |
35 | 41 | from quart import Quart, jsonify, request, send_from_directory |
36 | 42 | from quart_cors import cors |
37 | | - |
38 | 43 | # Import Pydantic models and service |
39 | | -from tasks import Task, TaskCreate, TaskFilter, TaskService, TaskStats, TaskUpdate |
| 44 | +from tasks import (Task, TaskCreate, TaskFilter, TaskService, TaskStats, |
| 45 | + TaskUpdate) |
40 | 46 |
|
41 | 47 | # ============================================================================ |
42 | 48 | # APPLICATION SETUP |
@@ -299,6 +305,130 @@ async def rest_list_ollama_models(): |
299 | 305 | return jsonify({"error": str(e)}), 500 |
300 | 306 |
|
301 | 307 |
|
| 308 | +# ============================================================================ |
| 309 | +# TICKET MCP EXAMPLE - Direct FastMCP client usage (no AI) |
| 310 | +# ============================================================================ |
| 311 | + |
| 312 | +async def _call_ticket_mcp_tool(tool_name: str, args: dict | None = None) -> list[dict]: |
| 313 | + """ |
| 314 | + Helper: Call a tool on the Ticket MCP server and extract results. |
| 315 | + |
| 316 | + This demonstrates using FastMCP client programmatically without any AI. |
| 317 | + The connection is opened, tool is called, and connection is closed. |
| 318 | + |
| 319 | + Args: |
| 320 | + tool_name: Name of the MCP tool to call (e.g., "list_tickets") |
| 321 | + args: Optional dict of arguments for the tool |
| 322 | + |
| 323 | + Returns: |
| 324 | + List of parsed JSON results from the tool response |
| 325 | + """ |
| 326 | + args = args or {} |
| 327 | + results = [] |
| 328 | + |
| 329 | + async with MCPClient(TICKET_MCP_SERVER_URL) as client: |
| 330 | + response = await client.call_tool(tool_name, args) |
| 331 | + |
| 332 | + # Extract text content from MCP response |
| 333 | + if hasattr(response, 'content') and response.content: |
| 334 | + for content_item in response.content: |
| 335 | + # Only process TextContent items (use getattr for type safety) |
| 336 | + text = getattr(content_item, 'text', None) |
| 337 | + if text is not None and isinstance(text, str): |
| 338 | + try: |
| 339 | + # Parse JSON if possible |
| 340 | + results.append(json.loads(text)) |
| 341 | + except json.JSONDecodeError: |
| 342 | + results.append({"text": text}) |
| 343 | + |
| 344 | + return results |
| 345 | + |
| 346 | + return results |
| 347 | + |
| 348 | + |
| 349 | +@app.route("/api/tickets", methods=["GET"]) |
| 350 | +async def rest_list_tickets(): |
| 351 | + """ |
| 352 | + List tickets from external Ticket MCP server. |
| 353 | + |
| 354 | + Example of calling MCP tools directly via FastMCP client. |
| 355 | + No AI involved - just pure MCP protocol. |
| 356 | + |
| 357 | + Query params: |
| 358 | + - status: Filter by status (new, assigned, in_progress, etc.) |
| 359 | + - priority: Filter by priority (critical, high, medium, low) |
| 360 | + - search: Full-text search in summary/description |
| 361 | + - page: Page number (default: 1) |
| 362 | + - page_size: Results per page (default: 20) |
| 363 | + """ |
| 364 | + try: |
| 365 | + # Build args from query params |
| 366 | + args = {} |
| 367 | + for param in ["status", "priority", "city", "service", "search"]: |
| 368 | + if val := request.args.get(param): |
| 369 | + args[param] = val |
| 370 | + for param in ["page", "page_size"]: |
| 371 | + if val := request.args.get(param): |
| 372 | + args[param] = int(val) |
| 373 | + |
| 374 | + results = await _call_ticket_mcp_tool("list_tickets", args) |
| 375 | + return jsonify(results[0] if len(results) == 1 else results), 200 |
| 376 | + except Exception as e: |
| 377 | + return jsonify({"error": str(e)}), 500 |
| 378 | + |
| 379 | + |
| 380 | +@app.route("/api/tickets/<ticket_id>", methods=["GET"]) |
| 381 | +async def rest_get_ticket(ticket_id: str): |
| 382 | + """ |
| 383 | + Get a single ticket by ID from the Ticket MCP server. |
| 384 | + |
| 385 | + Demonstrates calling MCP tool with path parameter. |
| 386 | + """ |
| 387 | + try: |
| 388 | + results = await _call_ticket_mcp_tool("get_ticket", {"ticket_id": ticket_id}) |
| 389 | + if not results: |
| 390 | + return jsonify({"error": "Ticket not found"}), 404 |
| 391 | + return jsonify(results[0]), 200 |
| 392 | + except Exception as e: |
| 393 | + return jsonify({"error": str(e)}), 500 |
| 394 | + |
| 395 | + |
| 396 | +@app.route("/api/tickets/stats", methods=["GET"]) |
| 397 | +async def rest_get_ticket_stats(): |
| 398 | + """ |
| 399 | + Get ticket statistics from the Ticket MCP server. |
| 400 | + |
| 401 | + Returns aggregated counts by status, priority, service, city. |
| 402 | + """ |
| 403 | + try: |
| 404 | + args = {} |
| 405 | + if time_from := request.args.get("time_from"): |
| 406 | + args["time_from"] = time_from |
| 407 | + if time_to := request.args.get("time_to"): |
| 408 | + args["time_to"] = time_to |
| 409 | + |
| 410 | + results = await _call_ticket_mcp_tool("get_ticket_stats", args) |
| 411 | + return jsonify(results[0] if len(results) == 1 else results), 200 |
| 412 | + except Exception as e: |
| 413 | + return jsonify({"error": str(e)}), 500 |
| 414 | + |
| 415 | + |
| 416 | +@app.route("/api/tickets/search", methods=["POST"]) |
| 417 | +async def rest_search_tickets(): |
| 418 | + """ |
| 419 | + Advanced ticket search with multiple filters. |
| 420 | + |
| 421 | + Body JSON: |
| 422 | + - query: Full-text search string |
| 423 | + - filters: {status: [...], priority: [...], city: [...], service: [...]} |
| 424 | + - limit: Max results |
| 425 | + """ |
| 426 | + try: |
| 427 | + data = await request.get_json() or {} |
| 428 | + results = await _call_ticket_mcp_tool("search_tickets", data) |
| 429 | + return jsonify(results[0] if len(results) == 1 else results), 200 |
| 430 | + except Exception as e: |
| 431 | + return jsonify({"error": str(e)}), 500 |
302 | 432 |
|
303 | 433 |
|
304 | 434 | # ============================================================================ |
|
0 commit comments