askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_mcp.py · 15.8 KB · 447 lines
| 1 | """Tests for the MCP endpoint and tool routing.""" |
| 2 | |
| 3 | import json |
| 4 | import urllib.parse |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | import object_mcp |
| 9 | import object_server |
| 10 | |
| 11 | from test_object_server import ( |
| 12 | TEST_ADMIN_TOKEN, |
| 13 | auth_headers, |
| 14 | create_identity_session, |
| 15 | enable_admin_token, |
| 16 | request, |
| 17 | raw_request, |
| 18 | write_records, |
| 19 | write_source, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def mcp_request(message, headers=None): |
| 24 | return request( |
| 25 | "/api/mcp", |
| 26 | method="POST", |
| 27 | body=json.dumps(message).encode(), |
| 28 | headers=headers if headers is not None else auth_headers(), |
| 29 | ) |
| 30 | |
| 31 | |
| 32 | def call_tool(name, arguments=None, headers=None, request_id=1): |
| 33 | status, _, payload = mcp_request( |
| 34 | { |
| 35 | "jsonrpc": "2.0", |
| 36 | "id": request_id, |
| 37 | "method": "tools/call", |
| 38 | "params": {"name": name, "arguments": arguments or {}}, |
| 39 | }, |
| 40 | headers=headers, |
| 41 | ) |
| 42 | assert status == 200 |
| 43 | result = payload["result"] |
| 44 | body = json.loads(result["content"][0]["text"]) |
| 45 | return result.get("isError", False), body |
| 46 | |
| 47 | |
| 48 | def test_mcp_requires_admin_gate(tmp_path, monkeypatch): |
| 49 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 50 | enable_admin_token(monkeypatch) |
| 51 | |
| 52 | status, _, payload = mcp_request( |
| 53 | {"jsonrpc": "2.0", "id": 1, "method": "tools/list"}, |
| 54 | headers=[], |
| 55 | ) |
| 56 | |
| 57 | assert status == 401 |
| 58 | assert payload["error"]["code"] == -32000 |
| 59 | assert payload["error"]["message"] == "Unauthorized" |
| 60 | |
| 61 | |
| 62 | def test_mcp_initialize_negotiates_protocol_and_session(tmp_path, monkeypatch): |
| 63 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 64 | enable_admin_token(monkeypatch) |
| 65 | |
| 66 | status, headers, payload = raw_request( |
| 67 | "/api/mcp", |
| 68 | method="POST", |
| 69 | body=json.dumps( |
| 70 | { |
| 71 | "jsonrpc": "2.0", |
| 72 | "id": 7, |
| 73 | "method": "initialize", |
| 74 | "params": {"protocolVersion": "2024-11-05"}, |
| 75 | } |
| 76 | ).encode(), |
| 77 | headers=auth_headers(), |
| 78 | ) |
| 79 | parsed = json.loads(payload) |
| 80 | |
| 81 | assert status == 200 |
| 82 | assert parsed["id"] == 7 |
| 83 | assert parsed["result"]["protocolVersion"] == "2024-11-05" |
| 84 | assert parsed["result"]["serverInfo"]["name"] == "dbbasic-object-server-mcp" |
| 85 | assert headers[b"mcp-session-id"] |
| 86 | |
| 87 | |
| 88 | def test_mcp_rejects_unsupported_protocol_header(tmp_path, monkeypatch): |
| 89 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 90 | enable_admin_token(monkeypatch) |
| 91 | |
| 92 | status, _, payload = mcp_request( |
| 93 | {"jsonrpc": "2.0", "id": 1, "method": "tools/list"}, |
| 94 | headers=[*auth_headers(), ("mcp-protocol-version", "1999-01-01")], |
| 95 | ) |
| 96 | |
| 97 | assert status == 400 |
| 98 | assert "Unsupported MCP protocol version" in payload["error"]["message"] |
| 99 | |
| 100 | |
| 101 | def test_mcp_notifications_return_202(tmp_path, monkeypatch): |
| 102 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 103 | enable_admin_token(monkeypatch) |
| 104 | |
| 105 | status, _, body = raw_request( |
| 106 | "/api/mcp", |
| 107 | method="POST", |
| 108 | body=json.dumps({"jsonrpc": "2.0", "method": "notifications/initialized"}).encode(), |
| 109 | headers=auth_headers(), |
| 110 | ) |
| 111 | |
| 112 | assert status == 202 |
| 113 | assert body == b"" |
| 114 | |
| 115 | |
| 116 | def test_mcp_tools_list_matches_catalog(tmp_path, monkeypatch): |
| 117 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 118 | enable_admin_token(monkeypatch) |
| 119 | |
| 120 | status, _, payload = mcp_request({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}) |
| 121 | |
| 122 | assert status == 200 |
| 123 | names = [tool["name"] for tool in payload["result"]["tools"]] |
| 124 | assert names == [tool["name"] for tool in object_mcp.TOOLS] |
| 125 | assert "execute_object" in names |
| 126 | assert all(tool["inputSchema"]["type"] == "object" for tool in payload["result"]["tools"]) |
| 127 | |
| 128 | |
| 129 | def test_mcp_unknown_method_and_bad_json(tmp_path, monkeypatch): |
| 130 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 131 | enable_admin_token(monkeypatch) |
| 132 | |
| 133 | unknown_status, _, unknown = mcp_request({"jsonrpc": "2.0", "id": 3, "method": "prompts/list"}) |
| 134 | parse_status, _, parse_error = request( |
| 135 | "/api/mcp", |
| 136 | method="POST", |
| 137 | body=b"{not json", |
| 138 | headers=auth_headers(), |
| 139 | ) |
| 140 | version_status, _, version_error = mcp_request({"jsonrpc": "1.0", "id": 4, "method": "tools/list"}) |
| 141 | |
| 142 | assert unknown_status == 200 |
| 143 | assert unknown["error"]["code"] == -32601 |
| 144 | assert parse_status == 200 |
| 145 | assert parse_error["error"]["code"] == -32700 |
| 146 | assert version_status == 200 |
| 147 | assert version_error["error"]["code"] == -32600 |
| 148 | |
| 149 | |
| 150 | def test_mcp_full_object_build_loop(tmp_path, monkeypatch): |
| 151 | root = tmp_path / "objects" |
| 152 | root.mkdir(parents=True) |
| 153 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 154 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path / "data")) |
| 155 | monkeypatch.setenv("DBBASIC_ENABLE_SOURCE_WRITES", "true") |
| 156 | monkeypatch.setenv(object_server.TRUSTED_IN_PROCESS_OBJECTS_ENV, "tools_greeter") |
| 157 | enable_admin_token(monkeypatch) |
| 158 | |
| 159 | created_error, created = call_tool( |
| 160 | "create_object", |
| 161 | { |
| 162 | "object_id": "tools_greeter", |
| 163 | "code": "def GET(request):\n return {'hello': request.get('name', 'world')}\n", |
| 164 | }, |
| 165 | ) |
| 166 | executed_error, executed = call_tool( |
| 167 | "execute_object", |
| 168 | {"object_id": "tools_greeter", "method": "GET", "payload": {"name": "mcp"}}, |
| 169 | ) |
| 170 | source_error, source = call_tool("get_object_source", {"object_id": "tools_greeter"}) |
| 171 | updated_error, updated = call_tool( |
| 172 | "update_object_source", |
| 173 | { |
| 174 | "object_id": "tools_greeter", |
| 175 | "code": "def GET(request):\n return {'hello': 'v2'}\n", |
| 176 | "message": "second version", |
| 177 | }, |
| 178 | ) |
| 179 | logs_error, logs = call_tool("get_object_logs", {"object_id": "tools_greeter", "limit": 10}) |
| 180 | |
| 181 | assert not created_error and created["http_status"] == 201 |
| 182 | assert not executed_error and executed["response"] == {"hello": "mcp"} |
| 183 | assert not source_error and "def GET" in source["response"]["source"] |
| 184 | assert not updated_error and updated["response"]["version_id"] == 2 |
| 185 | assert not logs_error and logs["http_status"] == 200 |
| 186 | |
| 187 | |
| 188 | def test_mcp_record_and_schema_tools(tmp_path, monkeypatch): |
| 189 | data_dir = tmp_path / "data" |
| 190 | write_records(data_dir, "contacts", "id\tname\nc1\tAda\n") |
| 191 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 192 | enable_admin_token(monkeypatch) |
| 193 | |
| 194 | schema_error, schema_set = call_tool( |
| 195 | "update_schema", |
| 196 | {"collection": "contacts", "schema": {"fields": [{"name": "id"}, {"name": "name"}]}}, |
| 197 | ) |
| 198 | created_error, created = call_tool( |
| 199 | "create_record", |
| 200 | {"collection": "contacts", "record": {"id": "c2", "name": "Grace"}}, |
| 201 | ) |
| 202 | listed_error, listed = call_tool("list_records", {"collection": "contacts", "limit": 10}) |
| 203 | updated_error, updated = call_tool( |
| 204 | "update_record", |
| 205 | {"collection": "contacts", "record_id": "c1", "changes": {"name": "Ada Lovelace"}}, |
| 206 | ) |
| 207 | deleted_error, deleted = call_tool("delete_record", {"collection": "contacts", "record_id": "c2"}) |
| 208 | changes_error, changes = call_tool("list_changes", {"kind": "record", "limit": 10}) |
| 209 | |
| 210 | assert not schema_error and schema_set["response"]["version_id"] == 1 |
| 211 | assert not created_error and created["http_status"] == 201 |
| 212 | assert not listed_error and listed["response"]["total"] == 2 |
| 213 | assert not updated_error and updated["response"]["record"]["name"] == "Ada Lovelace" |
| 214 | assert not deleted_error and deleted["response"]["deleted"] is True |
| 215 | assert not changes_error and changes["response"]["count"] >= 3 |
| 216 | |
| 217 | |
| 218 | def test_mcp_tool_route_translates_where_eq_shorthand_to_query_param(): |
| 219 | method, path, query, body = object_mcp.tool_route( |
| 220 | "list_records", {"collection": "contacts", "where": {"lead_status": "hot"}} |
| 221 | ) |
| 222 | |
| 223 | assert method == "GET" |
| 224 | assert path == "/admin/collections/contacts/records" |
| 225 | assert dict(urllib.parse.parse_qsl(query))["lead_status"] == "hot" |
| 226 | assert body == b"" |
| 227 | |
| 228 | |
| 229 | def test_mcp_tool_route_translates_where_explicit_op_to_dotted_param(): |
| 230 | method, path, query, body = object_mcp.tool_route( |
| 231 | "list_records", |
| 232 | {"collection": "contacts", "where": {"status": {"op": "in", "value": ["open", "assigned"]}}}, |
| 233 | ) |
| 234 | |
| 235 | assert dict(urllib.parse.parse_qsl(query))["status.in"] == "open,assigned" |
| 236 | |
| 237 | |
| 238 | def test_mcp_tool_route_translates_where_range_query_as_two_params(): |
| 239 | """A range (two conditions on the same field) round-trips as two |
| 240 | separate query params -- urlencode keeps repeated keys distinct, and |
| 241 | the HTTP handler ANDs same-field conditions from separate params.""" |
| 242 | method, path, query, body = object_mcp.tool_route( |
| 243 | "list_records", |
| 244 | { |
| 245 | "collection": "postings", |
| 246 | "where": { |
| 247 | "posted_at": [ |
| 248 | {"op": "gte", "value": "2026-07-01"}, |
| 249 | {"op": "lte", "value": "2026-07-31"}, |
| 250 | ] |
| 251 | }, |
| 252 | }, |
| 253 | ) |
| 254 | |
| 255 | pairs = urllib.parse.parse_qsl(query) |
| 256 | assert ("posted_at.gte", "2026-07-01") in pairs |
| 257 | assert ("posted_at.lte", "2026-07-31") in pairs |
| 258 | |
| 259 | |
| 260 | def test_mcp_tool_route_rejects_non_object_where(): |
| 261 | with pytest.raises(ValueError, match="where must be an object"): |
| 262 | object_mcp.tool_route("list_records", {"collection": "contacts", "where": "bogus"}) |
| 263 | |
| 264 | |
| 265 | def test_mcp_tool_route_rejects_where_condition_missing_op(): |
| 266 | with pytest.raises(ValueError, match="op"): |
| 267 | object_mcp.tool_route( |
| 268 | "list_records", {"collection": "contacts", "where": {"status": {"value": "hot"}}} |
| 269 | ) |
| 270 | |
| 271 | |
| 272 | def test_mcp_list_records_where_filters_end_to_end(tmp_path, monkeypatch): |
| 273 | data_dir = tmp_path / "data" |
| 274 | write_records( |
| 275 | data_dir, |
| 276 | "contacts", |
| 277 | "id\tname\tlead_status\nc1\tAda\thot\nc2\tGrace\tcold\nc3\tKatherine\thot\n", |
| 278 | ) |
| 279 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 280 | enable_admin_token(monkeypatch) |
| 281 | |
| 282 | error, result = call_tool( |
| 283 | "list_records", {"collection": "contacts", "where": {"lead_status": "hot"}} |
| 284 | ) |
| 285 | |
| 286 | assert not error |
| 287 | assert result["http_status"] == 200 |
| 288 | assert [r["id"] for r in result["response"]["records"]] == ["c1", "c3"] |
| 289 | |
| 290 | |
| 291 | def test_mcp_list_records_where_explicit_op_end_to_end(tmp_path, monkeypatch): |
| 292 | data_dir = tmp_path / "data" |
| 293 | write_records( |
| 294 | data_dir, |
| 295 | "contacts", |
| 296 | "id\tname\tstatus\nc1\tAda\topen\nc2\tGrace\tclosed\nc3\tKatherine\tassigned\n", |
| 297 | ) |
| 298 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 299 | enable_admin_token(monkeypatch) |
| 300 | |
| 301 | error, result = call_tool( |
| 302 | "list_records", |
| 303 | {"collection": "contacts", "where": {"status": {"op": "in", "value": ["open", "assigned"]}}}, |
| 304 | ) |
| 305 | |
| 306 | assert not error |
| 307 | assert [r["id"] for r in result["response"]["records"]] == ["c1", "c3"] |
| 308 | |
| 309 | |
| 310 | def test_mcp_list_records_where_unknown_field_surfaces_structured_400(tmp_path, monkeypatch): |
| 311 | """A bad filter still comes back as the same structured 400 a direct |
| 312 | HTTP caller gets -- MCP adds no new validation of its own, per this |
| 313 | module's "nothing new runs underneath" doctrine.""" |
| 314 | data_dir = tmp_path / "data" |
| 315 | write_records(data_dir, "contacts", "id\tname\nc1\tAda\n") |
| 316 | schema_file = data_dir / "schemas" / "contacts.json" |
| 317 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 318 | schema_file.write_text('{"fields": [{"name": "id"}, {"name": "name"}]}') |
| 319 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 320 | enable_admin_token(monkeypatch) |
| 321 | |
| 322 | error, result = call_tool( |
| 323 | "list_records", {"collection": "contacts", "where": {"nickname": "Ada"}} |
| 324 | ) |
| 325 | |
| 326 | assert error is True |
| 327 | assert result["http_status"] == 400 |
| 328 | assert result["response"]["code"] == "invalid_filter" |
| 329 | assert result["response"]["param"] == "nickname" |
| 330 | |
| 331 | |
| 332 | def test_mcp_global_search_tool(tmp_path, monkeypatch): |
| 333 | data_dir = tmp_path / "data" |
| 334 | write_records(data_dir, "notes", "id\tcontent\nn1\tflywheel growth loop\nn2\tother memo\n") |
| 335 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 336 | enable_admin_token(monkeypatch) |
| 337 | |
| 338 | call_tool( |
| 339 | "update_schema", |
| 340 | { |
| 341 | "collection": "notes", |
| 342 | "schema": { |
| 343 | "fields": [{"name": "id"}, {"name": "content"}], |
| 344 | "search": {"fields": ["content"]}, |
| 345 | }, |
| 346 | }, |
| 347 | ) |
| 348 | |
| 349 | search_error, found = call_tool("global_search", {"query": "flywheel growth"}) |
| 350 | scoped_error, scoped = call_tool( |
| 351 | "global_search", {"query": "flywheel", "collections": ["notes"], "limit": 5} |
| 352 | ) |
| 353 | |
| 354 | assert not search_error |
| 355 | assert found["response"]["results"]["notes"] == [ |
| 356 | {"id": "n1", "content": "flywheel growth loop"} |
| 357 | ] |
| 358 | assert found["response"]["total_count"] == 1 |
| 359 | assert not scoped_error and scoped["response"]["total_count"] == 1 |
| 360 | |
| 361 | |
| 362 | def test_global_search_tool_route_validates_arguments(): |
| 363 | with pytest.raises(ValueError, match="query"): |
| 364 | object_mcp.tool_route("global_search", {}) |
| 365 | with pytest.raises(ValueError, match="collections"): |
| 366 | object_mcp.tool_route("global_search", {"query": "x", "collections": "notes"}) |
| 367 | |
| 368 | method, path, query_string, body = object_mcp.tool_route( |
| 369 | "global_search", {"query": "fly wheel", "collections": ["notes", "tasks"]} |
| 370 | ) |
| 371 | assert (method, path, body) == ("GET", "/api/search", b"") |
| 372 | assert query_string == "q=fly+wheel&limit=10&collections=notes%2Ctasks" |
| 373 | |
| 374 | |
| 375 | def test_mcp_tool_errors_are_marked(tmp_path, monkeypatch): |
| 376 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 377 | enable_admin_token(monkeypatch) |
| 378 | |
| 379 | unknown_error, unknown = call_tool("launch_missiles", {}) |
| 380 | missing_error, missing = call_tool("get_object_source", {"object_id": "no_such_object"}) |
| 381 | unsafe_error, unsafe = call_tool("get_object_source", {"object_id": "../etc/passwd"}) |
| 382 | |
| 383 | assert unknown_error and "Unknown tool" in unknown["error"] |
| 384 | assert missing_error and missing["http_status"] == 404 |
| 385 | assert unsafe_error and "unsafe characters" in unsafe["error"] |
| 386 | |
| 387 | |
| 388 | def test_admin_gates_accept_session_cookie_when_gates_enabled(tmp_path, monkeypatch): |
| 389 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 390 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 391 | enable_admin_token(monkeypatch) |
| 392 | request( |
| 393 | "/identity/users", |
| 394 | method="POST", |
| 395 | body=json.dumps({"user_id": "dan", "roles": ["admin"]}).encode(), |
| 396 | headers=auth_headers(), |
| 397 | ) |
| 398 | token, _ = create_identity_session({"user_id": "dan", "label": "browser"}) |
| 399 | |
| 400 | cookie_status, _, cookie_payload = request( |
| 401 | "/admin/status", |
| 402 | headers=[("cookie", f"dbbasic_session={token}")], |
| 403 | ) |
| 404 | metrics_status, _, _ = request( |
| 405 | "/health", |
| 406 | query_string="metrics=true", |
| 407 | headers=[("cookie", f"dbbasic_session={token}")], |
| 408 | ) |
| 409 | |
| 410 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "false") |
| 411 | gates_off_status, _, _ = request( |
| 412 | "/admin/status", |
| 413 | headers=[("cookie", f"dbbasic_session={token}")], |
| 414 | ) |
| 415 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 416 | admin_token_cookie_status, _, _ = request( |
| 417 | "/admin/status", |
| 418 | headers=[("cookie", f"dbbasic_session={TEST_ADMIN_TOKEN}")], |
| 419 | ) |
| 420 | |
| 421 | assert cookie_status == 200 |
| 422 | assert cookie_payload["status"] == "ok" |
| 423 | assert metrics_status == 200 |
| 424 | assert gates_off_status == 401 |
| 425 | assert admin_token_cookie_status == 401 |
| 426 | |
| 427 | |
| 428 | def test_mcp_works_with_admin_role_session_token(tmp_path, monkeypatch): |
| 429 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 430 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 431 | enable_admin_token(monkeypatch) |
| 432 | request( |
| 433 | "/identity/users", |
| 434 | method="POST", |
| 435 | body=json.dumps({"user_id": "agent-claude", "roles": ["admin"]}).encode(), |
| 436 | headers=auth_headers(), |
| 437 | ) |
| 438 | token, _ = create_identity_session({"user_id": "agent-claude", "label": "mcp claude-code"}) |
| 439 | |
| 440 | is_error, body = call_tool( |
| 441 | "get_admin_status", |
| 442 | headers=[("authorization", f"Bearer {token}")], |
| 443 | ) |
| 444 | |
| 445 | assert not is_error |
| 446 | assert body["http_status"] == 200 |
| 447 | assert body["response"]["status"] == "ok" |