-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp_status.py
More file actions
256 lines (231 loc) · 9.33 KB
/
mcp_status.py
File metadata and controls
256 lines (231 loc) · 9.33 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
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
"""handlers/mcp_status.py — Server/MCP introspection tools.
These tools report on the MCP server itself (the Python process running
`server.py`), not the Couchbase cluster. They're useful for verifying that
configuration was applied correctly and for diagnosing why a tool is missing
from the discovered tool list (read-only mode, disabled-tools, etc.).
All tools here are READ ONLY and have no cluster dependency — they answer
purely from in-process state.
Tools:
cb_mcp_status High-level config summary (transport, safety flags,
tool counts, cluster auth method)
cb_mcp_list_tools List the tools currently exposed by this server
(post read-only / disabled filtering)
cb_mcp_get_tool_info Get the schema + annotations for a single tool by name
"""
from __future__ import annotations
import os
import sys
from mcp.types import TextContent, Tool, ToolAnnotations
from .shared import (
DISABLED_TOOLS,
ELICITATION_HINTS,
READ_ONLY_MODE,
err,
get_cluster_version,
ok,
)
TOOLS: list[Tool] = [
Tool(
name="cb_mcp_status",
description=(
"Get the current configuration of this MCP server: safety mode, "
"transport, cluster auth method, tool counts. Does not require a "
"live cluster connection."
),
inputSchema={"type": "object", "properties": {}},
annotations=ToolAnnotations(
readOnlyHint=True,
destructiveHint=False,
idempotentHint=True,
),
),
Tool(
name="cb_mcp_list_tools",
description=(
"List every tool currently exposed by this MCP server (after "
"read-only and disabled-tools filtering). Returns tool name plus "
"destructive / read-only annotations for each."
),
inputSchema={
"type": "object",
"properties": {
"category": {
"type": "string",
"description": (
"Optional category filter: 'read', 'write', "
"'destructive', or 'all' (default)."
),
"enum": ["read", "write", "destructive", "all"],
},
},
},
annotations=ToolAnnotations(
readOnlyHint=True,
destructiveHint=False,
idempotentHint=True,
),
),
Tool(
name="cb_mcp_get_tool_info",
description=(
"Get the input schema and annotations for a single tool. Useful "
"for inspecting required parameters before calling a tool."
),
inputSchema={
"type": "object",
"properties": {
"tool_name": {"type": "string"},
},
"required": ["tool_name"],
},
annotations=ToolAnnotations(
readOnlyHint=True,
destructiveHint=False,
idempotentHint=True,
),
),
]
def _auth_method() -> str:
"""Determine which auth method is configured."""
cert = os.environ.get("CB_CLIENT_CERT_PATH")
key = os.environ.get("CB_CLIENT_KEY_PATH")
if cert and key:
return "mTLS (client certificate)"
if os.environ.get("CB_USERNAME") and os.environ.get("CB_PASSWORD"):
return "Password (username/password)"
return "Not configured"
def _tls_state() -> dict:
"""Report TLS configuration without exposing credential paths."""
conn = os.environ.get("CB_CONNECTION_STRING", "couchbase://localhost")
is_tls = "couchbases://" in conn
insecure = os.environ.get("CB_MCP_TLS_INSECURE", "").lower() in ("1", "true", "yes")
return {
"tls_enabled": is_tls,
"tls_verify_disabled": insecure,
"ca_cert_configured": bool(os.environ.get("CB_CA_CERT_PATH")),
"client_cert_configured": bool(
os.environ.get("CB_CLIENT_CERT_PATH")
and os.environ.get("CB_CLIENT_KEY_PATH")
),
}
def _status_payload(server_module) -> dict:
"""Build the cb_mcp_status payload from in-process server state."""
raw_tools = getattr(server_module, "_RAW_TOOLS", [])
loaded_tools = getattr(server_module, "_TOOLS", [])
confirmation_required = getattr(server_module, "_CONFIRMATION_REQUIRED", set())
by_category = {
"read": sum(
1 for t in loaded_tools if t.annotations and t.annotations.readOnlyHint
),
"write": sum(
1 for t in loaded_tools if t.annotations and not t.annotations.readOnlyHint
),
"destructive": sum(
1 for t in loaded_tools if t.annotations and t.annotations.destructiveHint
),
}
return {
"server": "couchbase-mcp",
"python_version": sys.version.split()[0],
"transport": os.environ.get("CB_MCP_TRANSPORT", "stdio").lower(),
"transport_host": os.environ.get("CB_MCP_HOST", "127.0.0.1"),
"transport_port": int(os.environ.get("CB_MCP_PORT", "8000")),
"safety": {
"read_only_mode": READ_ONLY_MODE,
"elicitation_hints": ELICITATION_HINTS,
"disabled_tools_count": len(DISABLED_TOOLS),
"disabled_tools": sorted(DISABLED_TOOLS) if DISABLED_TOOLS else [],
"confirmation_required_count": len(confirmation_required),
},
"tools": {
"registered": len(raw_tools),
"loaded": len(loaded_tools),
"filtered_out": len(raw_tools) - len(loaded_tools),
"by_category": by_category,
},
"connection": {
"connection_string": os.environ.get(
"CB_CONNECTION_STRING", "couchbase://localhost"
),
"default_bucket": os.environ.get("CB_BUCKET", "default"),
"default_scope": os.environ.get("CB_SCOPE", "_default"),
"default_collection": os.environ.get("CB_COLLECTION", "_default"),
"auth_method": _auth_method(),
"tls": _tls_state(),
},
"cluster_version": get_cluster_version() or "unknown (not yet probed)",
"http_retries": int(os.environ.get("CB_MCP_HTTP_RETRIES", "3")),
"http_timeout_seconds": int(os.environ.get("CB_MCP_HTTP_TIMEOUT", "30")),
}
def _category_of(t: Tool) -> str:
"""Classify a single Tool for the cb_mcp_list_tools filter."""
if not t.annotations:
return "write"
if t.annotations.destructiveHint:
return "destructive"
if t.annotations.readOnlyHint:
return "read"
return "write"
def handle(name: str, args: dict) -> list[TextContent]:
try:
# Import server here (lazily) to avoid a circular import at module load.
import server as server_module
if name == "cb_mcp_status":
return ok(_status_payload(server_module))
if name == "cb_mcp_list_tools":
category = args.get("category", "all")
loaded_tools = getattr(server_module, "_TOOLS", [])
rows = []
for t in loaded_tools:
cat = _category_of(t)
if category != "all" and cat != category: # noqa: PLR1714
continue
rows.append(
{
"name": t.name,
"category": cat,
"read_only": bool(t.annotations and t.annotations.readOnlyHint),
"destructive": bool(
t.annotations and t.annotations.destructiveHint
),
"idempotent": bool(
t.annotations and t.annotations.idempotentHint
),
}
)
return ok({"count": len(rows), "filter": category, "tools": rows})
if name == "cb_mcp_get_tool_info":
target = args["tool_name"]
raw_tools = getattr(server_module, "_RAW_TOOLS", [])
loaded_tools = getattr(server_module, "_TOOLS", [])
loaded_names = {t.name for t in loaded_tools}
match = next((t for t in raw_tools if t.name == target), None)
if match is None:
return err(
f"No tool named {target!r} is registered with this server.",
tool=name,
hint="Use cb_mcp_list_tools to see available tools.",
)
return ok(
{
"name": match.name,
"description": match.description,
"input_schema": match.inputSchema,
"annotations": {
"read_only": bool(
match.annotations and match.annotations.readOnlyHint
),
"destructive": bool(
match.annotations and match.annotations.destructiveHint
),
"idempotent": bool(
match.annotations and match.annotations.idempotentHint
),
},
"currently_loaded": match.name in loaded_names,
"currently_disabled": match.name in DISABLED_TOOLS,
}
)
return err(f"Unknown mcp_status tool: {name}", tool=name)
except Exception as exc:
return err(f"{type(exc).__name__}: {exc}", tool=name, args=args)