-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinfo.py
More file actions
122 lines (106 loc) · 4.36 KB
/
info.py
File metadata and controls
122 lines (106 loc) · 4.36 KB
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
"""Server diagnostic tools."""
import logging
import os
import platform
import socket
import sys
from datetime import datetime, timezone
from typing import Any
from fastmcp import Context # pylint: disable=import-error
from ..mcp_core import getSessionHash, mcp
from ..memory_store import MCP_BASE_URL, MCP_OPENAPI_ENV
logging.getLogger(__name__).debug("module loaded")
# All instance identity fields are captured once at startup.
# Each running process (local dev, staging, Cloud Run) will show
# different values, making instances distinguishable at a glance.
_SERVER_START = datetime.now(timezone.utc)
_INSTANCE_HOST = socket.gethostname()
_INSTANCE_USER = os.environ.get("USER") or os.environ.get("USERNAME") or "unknown"
_INSTANCE_PID = os.getpid()
# Human-readable label: "alice@macbook-pro" locally, pod name on k8s/Cloud Run
_INSTANCE_LABEL = f"{_INSTANCE_USER}@{_INSTANCE_HOST}"
try:
from importlib.metadata import version as _pkg_version
_FASTMCP_VERSION = _pkg_version("fastmcp")
except Exception:
_FASTMCP_VERSION = "unknown"
_SERVER_VERSION = "0.2.0"
_SERVER_NAME = "Openapi.com MCP Gateway"
def _mask_token(token: str) -> str:
"""Show only the first 8 and last 4 chars; mask the rest."""
if len(token) <= 12:
return token[:4] + "..." + token[-2:]
return token[:8] + "..." + token[-4:]
@mcp.tool
async def openapi_server_info(ctx: Context) -> Any:
"""
Call this tool whenever the user asks:
- "are you connected to openapi?" / "sei connesso a openapi?"
- "which server / instance is running?"
- "what is your status?" / "are the tools working?"
- any question about connectivity, health, or identity of this MCP server.
Returns live diagnostic data directly from the running process — no external
API calls are made. The response includes:
- server name, version, and mode (sandbox vs production)
- instance identity: user@hostname, PID, and startup timestamp so different
running instances (local dev, staging, Cloud Run) are immediately distinguishable
- token status: whether OPENAPI_TOKEN / OPENAPI_SANDBOX_TOKEN are set in the
server environment, with a masked preview of the active token
- list of all MCP tools currently registered and available
- session identifiers for the current request
"""
now = datetime.now(timezone.utc)
uptime_seconds = int((now - _SERVER_START).total_seconds())
sandbox_mode = bool(MCP_OPENAPI_ENV)
# Token presence and masked preview — never expose the full token
prod_token = os.environ.get("OPENAPI_TOKEN", "")
sandbox_token = os.environ.get("OPENAPI_SANDBOX_TOKEN", "")
token_info = {
"OPENAPI_TOKEN": "set" if prod_token else "not set",
"OPENAPI_SANDBOX_TOKEN": "set" if sandbox_token else "not set",
"active_token_preview": _mask_token(sandbox_token if sandbox_mode else prod_token)
if (sandbox_token if sandbox_mode else prod_token) else None,
}
# Collect registered tool names from the MCP instance
try:
tool_names = sorted(t.name for t in await mcp.list_tools())
except Exception:
tool_names = []
return {
"server": {
"name": _SERVER_NAME,
"version": _SERVER_VERSION,
"mode": "sandbox" if sandbox_mode else "production",
"base_url": MCP_BASE_URL,
"description": mcp.instructions,
},
"instance": {
"label": _INSTANCE_LABEL,
"host": _INSTANCE_HOST,
"user": _INSTANCE_USER,
"pid": _INSTANCE_PID,
"started_at": _SERVER_START.isoformat(),
},
"token": token_info,
"runtime": {
"python": sys.version,
"platform": platform.platform(),
"fastmcp": _FASTMCP_VERSION,
},
"uptime": {
"started_at": _SERVER_START.isoformat(),
"checked_at": now.isoformat(),
"uptime_seconds": uptime_seconds,
},
"session": {
"request_id": ctx.request_id,
"session_id": ctx.session_id,
"client_id": ctx.client_id or "unknown",
"session_hash": getSessionHash(ctx),
},
"tools": {
"count": len(tool_names),
"names": tool_names,
},
"status": "ok",
}