File size: 13,405 Bytes
2f203f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import hashlib
import json
import logging
import os
import uuid

from contextlib import asynccontextmanager
from datetime import UTC, datetime
from typing import Any, Dict, List, Optional

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from data360.mcp_server.resources import CORSStaticFiles
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request as StarletteRequest

from data360.config import get_mcp_server_settings, setup_logging
from data360.health import get_liveness_body, run_readiness
from data360.http_client import aclose_shared_httpx_client
from data360.otel_setup import (
    configure_open_telemetry_for_server,
    instrument_httpx_outbound,
)

_audit_logger = logging.getLogger("audit")
_telemetry_client = None

# Setup logging from configuration
import sys

_logger = logging.getLogger(__name__)
mcp_settings = get_mcp_server_settings()

# Parse port from argv, supporting both '--port 8021' and '--port=8021' forms.
_parsed_port: int | None = None
for _arg in sys.argv:
    if _arg.startswith("--port="):
        try:
            _parsed_port = int(_arg.split("=", 1)[1])
        except ValueError:
            logging.warning("Could not parse port from argv argument '%s'; using default.", _arg)
        break
    if _arg == "--port":
        _idx = sys.argv.index(_arg)
        try:
            _parsed_port = int(sys.argv[_idx + 1])
        except (ValueError, IndexError):
            logging.warning("Could not parse port after '--port' in argv; using default.")
        break
if _parsed_port is not None:
    mcp_settings.port = _parsed_port

setup_logging(
    log_file=mcp_settings.log_file,
    log_level=mcp_settings.log_level,
    env=mcp_settings.env,
    azure_connection_string=mcp_settings.azure_connection_string,
)

# Tracer export (Azure in deployed envs; optional OTLP/console when MCP_ENV=local) and httpx spans
configure_open_telemetry_for_server(mcp_settings)
instrument_httpx_outbound()

# Import MCP after telemetry so the process uses an instrumented httpx from the first request.
from data360.mcp_server import mcp  # noqa: E402

_connection_string = mcp_settings.azure_connection_string or os.environ.get(
    "APPLICATIONINSIGHTS_CONNECTION_STRING"
)

# Initialize OpenCensus TelemetryClient for custom events (forwarded to Splunk)
if mcp_settings.env != "local" and _connection_string:
    try:
        from opencensus.ext.azure.log_exporter import (
            AzureEventHandler,  # type: ignore[import-untyped]
        )

        # Create a dedicated logger for custom events
        event_logger = logging.getLogger("customEvents")
        event_logger.setLevel(logging.INFO)

        # Add Azure handler that sends to customEvents table
        azure_handler = AzureEventHandler(connection_string=_connection_string)
        event_logger.addHandler(azure_handler)

        _telemetry_client = event_logger
    except ImportError:
        pass


class SecurityValidationMiddleware(BaseHTTPMiddleware):
    """Validate MCP tool calls to prevent prompt injection and unauthorized access."""

    async def dispatch(self, request: Request, call_next):
        # Only validate MCP JSON-RPC tool calls (not health probes under /mcp/*)
        if request.url.path in ("/mcp/health", "/mcp/ready"):
            return await call_next(request)
        if not request.url.path.startswith("/mcp"):
            return await call_next(request)

        try:
            # Read and parse body
            body_bytes = await request.body()
            if not body_bytes:
                return await call_next(request)

            body = json.loads(body_bytes)
            method = body.get("method", "")

            # Log tools/list requests for monitoring (allowed but monitored)
            if method == "tools/list":
                client_ip = request.headers.get(
                    "X-Forwarded-For",
                    request.client.host if request.client else "unknown",
                )
                logging.info(f"tools/list called from IP: {client_ip}")

            # Validate tools/call requests
            if method == "tools/call":
                from data360.mcp_server.security_validator import (  # noqa: PLC0415
                    validate_search_arguments,
                    validate_tool_call,
                )

                params = body.get("params", {})
                tool_name = params.get("name", "")
                arguments = params.get("arguments", {})

                # Validate tool call
                is_valid, error_msg = validate_tool_call(tool_name, arguments)
                if not is_valid:
                    logging.warning(
                        f"Security violation: {error_msg} | Tool: {tool_name}"
                    )
                    return JSONResponse(
                        status_code=403,
                        content={
                            "jsonrpc": "2.0",
                            "id": body.get("id"),
                            "error": {"code": -32001, "message": error_msg},
                        },
                    )

                # Additional validation for all search term inputs
                if tool_name == "data360_search_indicators":
                    is_valid, error_msg = validate_search_arguments(arguments)
                    if not is_valid:
                        logging.warning(
                            "Search query blocked: %s",
                            str(arguments)[:200],
                        )
                        return JSONResponse(
                            status_code=403,
                            content={
                                "jsonrpc": "2.0",
                                "id": body.get("id"),
                                "error": {"code": -32001, "message": error_msg},
                            },
                        )

        except json.JSONDecodeError:
            pass  # Let MCP handle invalid JSON
        except Exception:
            logging.error("Security validation error", exc_info=True)
            # Continue on validation errors to avoid blocking legitimate requests

        return await call_next(request)


class AuditLogMiddleware(BaseHTTPMiddleware):
    """Log structured audit entries for every MCP request."""

    async def dispatch(self, request: Request, call_next):
        # Only audit MCP JSON-RPC calls (not health probes)
        if request.url.path in ("/mcp/health", "/mcp/ready"):
            return await call_next(request)
        if not request.url.path.startswith("/mcp"):
            return await call_next(request)
        session_id = str(uuid.uuid4())
        requestor_id = request.headers.get(
            "X-Forwarded-For", request.client.host if request.client else "unknown"
        )
        timestamp = datetime.now(UTC).isoformat()
        # Read and restore body so downstream handlers still receive it
        body_bytes = await request.body()
        prompt = ""
        prompt_hash = ""
        try:
            body = json.loads(body_bytes)
            method = body.get("method", "")
            params = body.get("params", {})
            prompt = json.dumps(
                {"method": method, "params": params}, separators=(",", ":")
            )
            prompt_hash = hashlib.sha256(prompt.encode()).hexdigest()[:16]
        except Exception:
            pass
        response = await call_next(request)

        properties = {
            "session_id": session_id,
            "requestor_id": requestor_id,
            "timestamp": timestamp,
            "prompt": prompt,
            "prompt_hash": prompt_hash,
            "status_code": str(response.status_code),
            "path": request.url.path,
        }

        # Log to traces with custom dimensions
        _audit_logger.info("mcp_audit", extra={"custom_dimensions": properties})

        # Also log as custom event for Splunk forwarding
        if _telemetry_client:
            _telemetry_client.info(
                "MCP_Request",
                extra={
                    "custom_dimensions": properties,
                    "event_name": "MCP_Request",
                },
            )

        return response


from fastmcp import settings
settings.stateless_http = True

# NOTE: import to be able to run the server with all definitions loaded
# path="/mcp" means the MCP endpoint lives at /mcp (no trailing slash needed)
mcp_app = mcp.http_app(path="/mcp")


async def health_check(request: StarletteRequest) -> JSONResponse:
    """Liveness probe under the MCP URL prefix (GET /mcp/health)."""
    del request
    return JSONResponse(get_liveness_body())


async def ready_check(request: StarletteRequest) -> JSONResponse:
    """Readiness probe under the MCP URL prefix (GET /mcp/ready)."""
    del request
    status_code, body = await run_readiness()
    return JSONResponse(content=body, status_code=status_code)


# Starlette routes on mcp_app: paths are absolute from mount root (not nested under /mcp).
# Use /mcp/health so probes sit beside the streamable HTTP endpoint at /mcp.
mcp_app.add_route("/mcp/health", health_check, methods=["GET", "HEAD"])
mcp_app.add_route("/mcp/ready", ready_check, methods=["GET", "HEAD"])


@asynccontextmanager
async def _lifespan_with_http_cleanup(app: FastAPI):
    """Run MCP startup/shutdown, then close the shared httpx client."""
    async with mcp_app.router.lifespan_context(mcp_app):
        yield
    await aclose_shared_httpx_client()


# https://gofastmcp.com/deployment/http#asgi-application
# redirect_slashes=False prevents 308 redirects between /mcp and /mcp/
app = FastAPI(
    title="Data360 MCP Server",
    lifespan=_lifespan_with_http_cleanup,
    redirect_slashes=False,
)  # pyright: ignore[reportUnusedExpression]

app.add_middleware(AuditLogMiddleware)
# SecurityValidationMiddleware is enabled for incoming request validation.
app.add_middleware(SecurityValidationMiddleware)

from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Instrument FastAPI for incoming request tracking
if mcp_settings.env != "local" and _connection_string:
    try:
        from opentelemetry.instrumentation.fastapi import (
            FastAPIInstrumentor,  # type: ignore[import-untyped]
        )

        FastAPIInstrumentor.instrument_app(app)
    except ImportError:
        pass


@app.get("/")
async def root():
    return {
        "service": "data360-mcp",
        "health": "/mcp/health",
        "ready": "/mcp/ready",
        "mcp": "/mcp",
    }



class VizSpecRequest(BaseModel):
    database_id: str
    indicator_id: str
    country_code: str | None = None
    start_year: int | None = None
    end_year: int | None = None
    disaggregation_filters: dict[str, str | None] | None = None
    chart_type: str | None = None
    relevant_fields: list[str] | None = None
    chart_title: str | None = None
    series_labels: dict[str, str] | None = None


@app.post("/api/viz-spec")
async def get_viz_spec_endpoint(req: VizSpecRequest):
    from data360 import visualization as data360_viz

    try:
        # Pass charts_api_url_override=None to force local static file storage,
        # avoiding mutation of the shared @ft.cache singleton (which is a race condition
        # under concurrent requests). The HTML app always wants local specs for direct
        # file access; blob storage routing happens via the MCP tool path, not this endpoint.
        res = await data360_viz.get_viz_spec(
            database_id=req.database_id,
            indicator_id=req.indicator_id,
            country_code=req.country_code,
            start_year=req.start_year,
            end_year=req.end_year,
            disaggregation_filters=req.disaggregation_filters,
            chart_type=req.chart_type,
            relevant_fields=req.relevant_fields,
            chart_title=req.chart_title,
            series_labels=req.series_labels,
            charts_api_url_override=None,
        )
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": str(e)})

    if res.get("error"):
        return JSONResponse(status_code=400, content={"error": res.get("error")})

    spec = res.get("spec")
    if not spec:
        return JSONResponse(status_code=500, content={"error": "Vega-Lite spec was not generated."})

    return {k: v for k, v in {
        "spec": spec,
        "reason": res.get("reason"),
        "strategy": res.get("strategy"),
    }.items() if v is not None}




if os.environ.get("PYTEST_CURRENT_TEST"):
    static_dir = os.path.join(os.getcwd(), "static")
else:
    server_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.abspath(os.path.join(server_dir, "..", ".."))
    static_dir = os.path.join(project_root, "static")
os.makedirs(static_dir, exist_ok=True)
app.mount("/static", CORSStaticFiles(directory=static_dir), name="static")
# Mount MCP app at root — the path="/mcp" in http_app() handles the /mcp route
app.mount("/", mcp_app)


# Tools and other resources are automatically registered via imports in mcp_server/__init__.py
# See src/data360/mcp_server/tools.py for tool definitions