agentcat 2.0.0b2__tar.gz → 2.0.0b3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/PKG-INFO +5 -3
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/README.md +4 -2
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/pyproject.toml +1 -1
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/callpath.py +1 -1
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/handles.py +2 -4
- agentcat-2.0.0b3/src/agentcat/modules/hooks.py +118 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/identify.py +10 -2
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/internal.py +3 -7
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/redaction.py +7 -10
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/types.py +13 -1
- agentcat-2.0.0b3/tests/e2e/community_v3/test_agent_handle_http.py +203 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v3/test_event_capture_http.py +53 -4
- agentcat-2.0.0b3/tests/e2e/community_v3/test_identify_http.py +189 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v3/test_stateless_http.py +9 -0
- agentcat-2.0.0b3/tests/e2e/community_v4/test_agent_handle_http.py +202 -0
- agentcat-2.0.0b3/tests/e2e/community_v4/test_identify_http.py +199 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v4/test_session_http.py +50 -4
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v4/test_stateless_http.py +12 -2
- agentcat-2.0.0b3/tests/e2e/official/test_agent_handle_http.py +237 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_identify_http.py +24 -4
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_session_http.py +38 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_stateless_http.py +11 -0
- agentcat-2.0.0b3/tests/e2e/official_modern/test_agent_handle_http.py +198 -0
- agentcat-2.0.0b3/tests/e2e/official_modern/test_identify_http.py +231 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official_modern/test_session_http.py +96 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official_modern/test_stateless_http.py +18 -5
- agentcat-2.0.0b3/tests/test_async_hooks.py +319 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_identify_shape.py +57 -0
- agentcat-2.0.0b2/tests/e2e/community_v3/test_identify_http.py +0 -110
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/.gitignore +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/CONTRIBUTING.md +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/LICENSE +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/docs/cats/bibi.png +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/docs/cats/zelda.jpg +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/docs/static/architecture.png +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/docs/static/og-image.png +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/_common.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/_inner_tap.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/community.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/lowlevel_v1.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/adapters/lowlevel_v2.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/client_identity.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/constants.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/detection.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/diagnostics.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/event_queue.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exceptions.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exporters/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exporters/datadog.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exporters/otlp.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exporters/sentry.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/exporters/trace_context.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/injection.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/logging.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/request_extra.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/sanitization.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/telemetry.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/tools.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/truncation.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/modules/validation.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/thirdparty/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/thirdparty/ksuid/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/thirdparty/ksuid/baseconv.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/thirdparty/ksuid/ksuid.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/src/agentcat/utils.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/test_community_v3_event_serialization.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/test_community_v3_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/test_community_v3_inject_context.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/test_community_v3_openapi.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/community/test_community_v3_runtime_state_tools.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/_helpers.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v3/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v3/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v3/test_request_extra_http.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v4/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/community_v4/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_event_capture_http.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_redaction_http.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official/test_request_extra_http.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official_modern/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official_modern/conftest.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/e2e/official_modern/test_mcpserver_http.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_api_base_url.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_callpath.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_client_identity.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_community_v4_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_concurrency_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_constants_copy.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_customer_owned_parameters.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_detection.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_attributes.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_auth.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_export.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_integration.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_no_payload.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_optout.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_record.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_sink.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_diagnostics_test_env.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_dynamic_tracking.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_event_capture_completeness.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_event_queue.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_event_tags_properties.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_exceptions.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_exporter_sdk_version.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_injection.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_inner_tap.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_logging.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_lowlevel_v1_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_lowlevel_v2_handles.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_multiple_servers.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_options_v2.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_publish_custom_event.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_rebuild_on_demand.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_redaction.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_removed_events.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_report_missing.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_request_extra.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_response_shape.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_sanitization.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_session_id_validation.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_tag_validation.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_tool_context.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_truncation.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_truncation_json_safe.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/__init__.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/client.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/community_client.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/community_openapi_server.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/community_todo_server.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/delivery.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/flavors.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/modern_server.py +0 -0
- {agentcat-2.0.0b2 → agentcat-2.0.0b3}/tests/test_utils/todo_server.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: agentcat
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.0b3
|
|
4
4
|
Summary: Analytics tool for MCP (Model Context Protocol) servers and AI agents - tracks tool usage patterns and provides insights
|
|
5
5
|
Project-URL: Homepage, https://github.com/agentcathq/agentcat-python-sdk
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/agentcathq/agentcat-python-sdk/issues
|
|
@@ -77,7 +77,7 @@ pip install --pre "agentcat[community]"
|
|
|
77
77
|
```
|
|
78
78
|
|
|
79
79
|
`--pre` is required while 2.x is a prerelease: without it pip skips
|
|
80
|
-
`2.0.
|
|
80
|
+
`2.0.0b3` and resolves the 1.x line instead, which has none of the features
|
|
81
81
|
documented below. Drop the flag once a stable 2.0.0 ships.
|
|
82
82
|
|
|
83
83
|
One `track()` call covers every supported server shape:
|
|
@@ -203,6 +203,8 @@ def identify_user(request, extra):
|
|
|
203
203
|
agentcat.track(server, "proj_0000000", AgentCatOptions(identify=identify_user))
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
+
The hook may be sync or async — `async def identify_user(request, extra)` works the same way, and is the better shape when the lookup is a network call, since a blocking one holds up every other tool call in flight. Returning anything that is not a `UserIdentity`, or raising, publishes the event anonymously rather than failing the call.
|
|
207
|
+
|
|
206
208
|
### Bringing your own session IDs
|
|
207
209
|
|
|
208
210
|
If you already have a correlation ID — a trace ID, a job ID, a header your gateway sets — hand it to AgentCat with `resolve_session_id` and no `session_id` parameter is injected into your tools at all:
|
|
@@ -305,7 +307,7 @@ Learn more about our free and open source [telemetry integrations](https://docs.
|
|
|
305
307
|
|
|
306
308
|
### Known limitations
|
|
307
309
|
|
|
308
|
-
Two behaviors are worth knowing before you read your first dashboard. Both are deliberate for `2.0.
|
|
310
|
+
Two behaviors are worth knowing before you read your first dashboard. Both are deliberate for `2.0.0b3`.
|
|
309
311
|
|
|
310
312
|
- **Multi-round tool calls that mint their own handle land on separate sessions.** When a tool call runs several round trips and the *first* round is what mints the handle, each round is attributed to its own session rather than one shared session. Supplying a `session_id` yourself, or deriving one with `resolve_session_id`, correlates the rounds correctly — those two modes are protocol-enforced. Only the mint-on-first-round case is affected.
|
|
311
313
|
- **Errors forwarded from a proxied community tool carry no stack detail.** When a community FastMCP server proxies a tool to an upstream server and that upstream returns an error result, there is no local Python exception to read, so the event records the error message without a stack trace. Errors raised by your own tool code are unaffected and carry full detail.
|
|
@@ -45,7 +45,7 @@ pip install --pre "agentcat[community]"
|
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
`--pre` is required while 2.x is a prerelease: without it pip skips
|
|
48
|
-
`2.0.
|
|
48
|
+
`2.0.0b3` and resolves the 1.x line instead, which has none of the features
|
|
49
49
|
documented below. Drop the flag once a stable 2.0.0 ships.
|
|
50
50
|
|
|
51
51
|
One `track()` call covers every supported server shape:
|
|
@@ -171,6 +171,8 @@ def identify_user(request, extra):
|
|
|
171
171
|
agentcat.track(server, "proj_0000000", AgentCatOptions(identify=identify_user))
|
|
172
172
|
```
|
|
173
173
|
|
|
174
|
+
The hook may be sync or async — `async def identify_user(request, extra)` works the same way, and is the better shape when the lookup is a network call, since a blocking one holds up every other tool call in flight. Returning anything that is not a `UserIdentity`, or raising, publishes the event anonymously rather than failing the call.
|
|
175
|
+
|
|
174
176
|
### Bringing your own session IDs
|
|
175
177
|
|
|
176
178
|
If you already have a correlation ID — a trace ID, a job ID, a header your gateway sets — hand it to AgentCat with `resolve_session_id` and no `session_id` parameter is injected into your tools at all:
|
|
@@ -273,7 +275,7 @@ Learn more about our free and open source [telemetry integrations](https://docs.
|
|
|
273
275
|
|
|
274
276
|
### Known limitations
|
|
275
277
|
|
|
276
|
-
Two behaviors are worth knowing before you read your first dashboard. Both are deliberate for `2.0.
|
|
278
|
+
Two behaviors are worth knowing before you read your first dashboard. Both are deliberate for `2.0.0b3`.
|
|
277
279
|
|
|
278
280
|
- **Multi-round tool calls that mint their own handle land on separate sessions.** When a tool call runs several round trips and the *first* round is what mints the handle, each round is attributed to its own session rather than one shared session. Supplying a `session_id` yourself, or deriving one with `resolve_session_id`, correlates the rounds correctly — those two modes are protocol-enforced. Only the mint-on-first-round case is affected.
|
|
279
281
|
- **Errors forwarded from a proxied community tool carry no stack detail.** When a community FastMCP server proxies a tool to an upstream server and that upstream returns an error result, there is no local Python exception to read, so the event records the error message without a stack trace. Errors raised by your own tool code are unaffected and carry full detail.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "agentcat"
|
|
3
|
-
version = "2.0.
|
|
3
|
+
version = "2.0.0b3"
|
|
4
4
|
description = "Analytics tool for MCP (Model Context Protocol) servers and AI agents - tracks tool usage patterns and provides insights"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "AgentCat, Inc.", email = "support@agentcat.com" },
|
|
@@ -122,7 +122,7 @@ async def resolve_call(
|
|
|
122
122
|
injected_parameter_names(tool_name, data.injected_params_registry),
|
|
123
123
|
tool_name not in data.declared_session_params,
|
|
124
124
|
)
|
|
125
|
-
actor = resolve_identity(data, request, extra)
|
|
125
|
+
actor = await resolve_identity(data, request, extra)
|
|
126
126
|
client = resolve_client_identity(meta_sources, legacy_client)
|
|
127
127
|
protocol_version = resolve_protocol_version(meta_sources, protocol_fallback)
|
|
128
128
|
# Read from the RAW arguments (pre-strip): the event records what the agent
|
|
@@ -6,7 +6,6 @@ splits customer sessions across an upgrade.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import hashlib
|
|
9
|
-
import inspect
|
|
10
9
|
import re
|
|
11
10
|
from dataclasses import dataclass
|
|
12
11
|
from typing import Any, Literal
|
|
@@ -28,6 +27,7 @@ from agentcat.modules.constants import (
|
|
|
28
27
|
mint_back_confirmed,
|
|
29
28
|
mint_back_session_line,
|
|
30
29
|
)
|
|
30
|
+
from agentcat.modules.hooks import await_hook_result
|
|
31
31
|
from agentcat.modules.logging import write_to_log
|
|
32
32
|
from agentcat.thirdparty.ksuid import Ksuid
|
|
33
33
|
from agentcat.types import AgentCatOptions
|
|
@@ -149,9 +149,7 @@ async def resolve_handles(
|
|
|
149
149
|
|
|
150
150
|
if callable(hook):
|
|
151
151
|
try:
|
|
152
|
-
value = hook(request, extra)
|
|
153
|
-
if inspect.isawaitable(value):
|
|
154
|
-
value = await value
|
|
152
|
+
value = await await_hook_result(hook(request, extra), "resolve_session_id")
|
|
155
153
|
except Exception as e: # hook errors mint silently
|
|
156
154
|
write_to_log(f"Warning: resolve_session_id hook raised: {e}")
|
|
157
155
|
value = None
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""What "may be sync or async" means, defined once for every customer hook.
|
|
2
|
+
|
|
3
|
+
`AgentCatOptions` takes five customer-supplied callables — `identify`,
|
|
4
|
+
`event_tags`, `event_properties`, `resolve_session_id` and
|
|
5
|
+
`redact_sensitive_information` — and all five are documented to accept a sync
|
|
6
|
+
or an async function. Before this module each site answered that question for
|
|
7
|
+
itself, in four different spellings across four files, and `identify` did not
|
|
8
|
+
answer it at all: it called the hook and used the return value verbatim, so an
|
|
9
|
+
`async def identify` built a coroutine, ran none of its body, failed the
|
|
10
|
+
`isinstance(result, UserIdentity)` check and published the call anonymously.
|
|
11
|
+
The customer saw no error — only an event with no actor.
|
|
12
|
+
|
|
13
|
+
Two entry points, because the SDK runs hooks from two places:
|
|
14
|
+
|
|
15
|
+
* `await_hook_result` — the request path. Every adapter reaches the hooks
|
|
16
|
+
through `await resolve_call(...)` and `await publish_tool_call_event(...)`,
|
|
17
|
+
on every mcp and fastmcp version in the compatibility matrix, so there is a
|
|
18
|
+
running loop and awaiting is all it takes.
|
|
19
|
+
* `drive_hook_result` — the publish worker (`event_queue.EventQueue._worker`),
|
|
20
|
+
a daemon THREAD with no event loop of its own. Redaction runs there and
|
|
21
|
+
cannot await, so an awaitable has to be driven to completion with a loop of
|
|
22
|
+
its own.
|
|
23
|
+
|
|
24
|
+
Both narrow on `inspect.isawaitable`, never `inspect.iscoroutine`. The
|
|
25
|
+
difference is not academic: `iscoroutine` matches only native coroutines, so a
|
|
26
|
+
hook returning an `asyncio.Task` or `Future` — a cached in-flight lookup, say —
|
|
27
|
+
or any object implementing `__await__` would be assigned into the event
|
|
28
|
+
verbatim. That is the `<coroutine object ...>`-shaped failure again, and it
|
|
29
|
+
looks like the hook worked.
|
|
30
|
+
|
|
31
|
+
Both also narrow on the RESULT rather than the callable. `iscoroutinefunction`
|
|
32
|
+
would be wrong here: it answers False for a `functools.partial` of an async
|
|
33
|
+
function, for a bound method of one on some versions, and for any decorator
|
|
34
|
+
that returns a non-async wrapper around async work. Calling the hook and asking
|
|
35
|
+
what came back needs none of those special cases.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
import asyncio
|
|
39
|
+
import inspect
|
|
40
|
+
from collections.abc import Awaitable
|
|
41
|
+
from typing import Any, TypeVar, cast
|
|
42
|
+
|
|
43
|
+
from agentcat.modules.logging import write_to_log
|
|
44
|
+
|
|
45
|
+
T = TypeVar("T")
|
|
46
|
+
|
|
47
|
+
# Both signatures below spell `T | Awaitable[Any]` inline rather than sharing an
|
|
48
|
+
# alias. A generic alias needs `Union[T, ...]` — mypy rejects a TypeVar as the
|
|
49
|
+
# target of a PEP-604 alias — and ruff's UP007 then rewrites that `Union` back
|
|
50
|
+
# into the form mypy rejected. Inline, both tools agree.
|
|
51
|
+
#
|
|
52
|
+
# `Any` rather than `T` on the awaitable half: a customer's hook is untyped at
|
|
53
|
+
# runtime and its option alias is itself a union, so the concrete awaitable is
|
|
54
|
+
# whatever they returned.
|
|
55
|
+
#
|
|
56
|
+
# Only the awaited branch casts. `inspect.isawaitable` is a TypeGuard, so its
|
|
57
|
+
# NEGATIVE arm already narrows the union to `T` on its own — a cast there is
|
|
58
|
+
# redundant, and mypy says so. Awaiting an `Awaitable[Any]` yields `Any`, which
|
|
59
|
+
# `warn_return_any` rejects, so that arm does need one.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _report(hook_name: str, error: Exception) -> None:
|
|
63
|
+
"""Name the await as the cause, since the caller's log cannot.
|
|
64
|
+
|
|
65
|
+
Every call site already logs "this hook failed, degrading" from its own
|
|
66
|
+
`except`. That line is true but unhelpful when the hook itself was fine and
|
|
67
|
+
only the awaiting went wrong — a customer reading it goes looking for a bug
|
|
68
|
+
in code that ran correctly. This one says which half broke; the caller's
|
|
69
|
+
still says which hook degraded, and both are wanted.
|
|
70
|
+
"""
|
|
71
|
+
write_to_log(
|
|
72
|
+
f"Warning: {hook_name} returned an awaitable that could not be awaited: {error}"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
async def await_hook_result(value: T | Awaitable[Any], hook_name: str) -> T:
|
|
77
|
+
"""Resolve a hook's return value on a path that has a running loop.
|
|
78
|
+
|
|
79
|
+
Re-raises rather than swallowing: each call site owns its own degradation
|
|
80
|
+
rule, and they differ. A failed `identify` yields an anonymous actor, a
|
|
81
|
+
failed `resolve_session_id` mints a fresh handle, a failed redaction drops
|
|
82
|
+
the event entirely. Deciding that here would flatten three deliberate
|
|
83
|
+
behaviors into one.
|
|
84
|
+
"""
|
|
85
|
+
if not inspect.isawaitable(value):
|
|
86
|
+
return value
|
|
87
|
+
try:
|
|
88
|
+
return cast(T, await value)
|
|
89
|
+
except Exception as e:
|
|
90
|
+
_report(hook_name, e)
|
|
91
|
+
raise
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def drive_hook_result(value: T | Awaitable[Any], hook_name: str) -> T:
|
|
95
|
+
"""Resolve a hook's return value from a thread with no event loop.
|
|
96
|
+
|
|
97
|
+
`asyncio.run` builds a loop, runs the awaitable to completion and tears the
|
|
98
|
+
loop down. That is only valid because the publish worker is a plain thread
|
|
99
|
+
that never had one — calling this from inside a running loop raises, which
|
|
100
|
+
is why the request path uses `await_hook_result` instead.
|
|
101
|
+
|
|
102
|
+
Not cheap, and knowingly so: the one caller is redaction, a security
|
|
103
|
+
control, running off the request's hot path. A hook that cannot be driven
|
|
104
|
+
raises here and the queue drops the event rather than publishing it
|
|
105
|
+
unredacted.
|
|
106
|
+
"""
|
|
107
|
+
if not inspect.isawaitable(value):
|
|
108
|
+
return value
|
|
109
|
+
try:
|
|
110
|
+
return cast(T, asyncio.run(_resolved(value)))
|
|
111
|
+
except Exception as e:
|
|
112
|
+
_report(hook_name, e)
|
|
113
|
+
raise
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async def _resolved(awaitable: Any) -> Any:
|
|
117
|
+
"""`asyncio.run` takes a coroutine, and an arbitrary awaitable is not one."""
|
|
118
|
+
return await awaitable
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
from typing import Any
|
|
2
2
|
|
|
3
|
+
from agentcat.modules.hooks import await_hook_result
|
|
3
4
|
from agentcat.modules.logging import write_to_log
|
|
4
5
|
from agentcat.types import AgentCatData, UserIdentity
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
def resolve_identity(
|
|
8
|
+
async def resolve_identity(
|
|
8
9
|
data: AgentCatData | None, request: Any, extra: Any
|
|
9
10
|
) -> UserIdentity | None:
|
|
10
11
|
"""Run the configured identify hook and return its UserIdentity.
|
|
@@ -14,12 +15,19 @@ def resolve_identity(
|
|
|
14
15
|
stamps the actor onto it, so identity resolution is pure and runs on every
|
|
15
16
|
call. Never raises — a customer hook that blows up yields an anonymous
|
|
16
17
|
call, not a failed one.
|
|
18
|
+
|
|
19
|
+
Async as of the sync-or-async sweep: the hook may be an `async def`, and
|
|
20
|
+
the await lives inside the same `try` as the call, so an awaitable that
|
|
21
|
+
fails degrades exactly like a hook that raised. Callers are unaffected —
|
|
22
|
+
the one call site (`callpath.resolve_call`) was already async.
|
|
17
23
|
"""
|
|
18
24
|
if not data or not data.options or not data.options.identify:
|
|
19
25
|
return None
|
|
20
26
|
|
|
21
27
|
try:
|
|
22
|
-
result =
|
|
28
|
+
result = await await_hook_result(
|
|
29
|
+
data.options.identify(request, extra), "identify"
|
|
30
|
+
)
|
|
23
31
|
except Exception as e:
|
|
24
32
|
write_to_log(f"Error occurred during user identification: {e}")
|
|
25
33
|
return None
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"""Internal data storage for AgentCat."""
|
|
2
2
|
|
|
3
|
-
import inspect
|
|
4
3
|
import weakref
|
|
5
4
|
from typing import Any, Dict, Optional
|
|
6
5
|
|
|
@@ -10,6 +9,7 @@ from ..types import AgentCatData, UnredactedEvent
|
|
|
10
9
|
# `_get_server_key`. detection.py imports nothing from this module, so this
|
|
11
10
|
# does not close a cycle.
|
|
12
11
|
from .detection import _probe as _attribute_present
|
|
12
|
+
from .hooks import await_hook_result
|
|
13
13
|
from .logging import write_to_log
|
|
14
14
|
from .validation import validate_tags
|
|
15
15
|
|
|
@@ -112,9 +112,7 @@ async def resolve_event_tags(
|
|
|
112
112
|
return None
|
|
113
113
|
|
|
114
114
|
try:
|
|
115
|
-
result = callback(request, extra)
|
|
116
|
-
if inspect.iscoroutine(result):
|
|
117
|
-
result = await result
|
|
115
|
+
result = await await_hook_result(callback(request, extra), "event_tags")
|
|
118
116
|
except Exception as e:
|
|
119
117
|
write_to_log(f"event_tags callback error: {e}")
|
|
120
118
|
return None
|
|
@@ -138,9 +136,7 @@ async def resolve_event_properties(
|
|
|
138
136
|
return None
|
|
139
137
|
|
|
140
138
|
try:
|
|
141
|
-
result = callback(request, extra)
|
|
142
|
-
if inspect.iscoroutine(result):
|
|
143
|
-
result = await result
|
|
139
|
+
result = await await_hook_result(callback(request, extra), "event_properties")
|
|
144
140
|
except Exception as e:
|
|
145
141
|
write_to_log(f"event_properties callback error: {e}")
|
|
146
142
|
return None
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"""PII redaction for AgentCat logs."""
|
|
2
2
|
|
|
3
|
-
import asyncio
|
|
4
|
-
import inspect
|
|
5
3
|
from typing import Any, TYPE_CHECKING, Callable, Set
|
|
6
4
|
|
|
5
|
+
from agentcat.modules.hooks import drive_hook_result
|
|
6
|
+
|
|
7
7
|
if TYPE_CHECKING:
|
|
8
8
|
from agentcat.types import Event, UnredactedEvent
|
|
9
9
|
|
|
@@ -89,10 +89,6 @@ def redact_strings_in_object(
|
|
|
89
89
|
return obj
|
|
90
90
|
|
|
91
91
|
|
|
92
|
-
async def _resolved(awaitable: Any) -> Any:
|
|
93
|
-
return await awaitable
|
|
94
|
-
|
|
95
|
-
|
|
96
92
|
def _sync_redactor(redact_fn: Callable[[str], Any]) -> Callable[[str], Any]:
|
|
97
93
|
"""A synchronous view of the customer's hook.
|
|
98
94
|
|
|
@@ -105,13 +101,14 @@ def _sync_redactor(redact_fn: Callable[[str], Any]) -> Callable[[str], Any]:
|
|
|
105
101
|
security control wins, and this runs off the request's hot path. A hook
|
|
106
102
|
that cannot be driven raises, and the queue drops the event rather than
|
|
107
103
|
publishing it unredacted.
|
|
104
|
+
|
|
105
|
+
The driving itself lives in `modules/hooks.py`, which is where all five
|
|
106
|
+
customer hooks get their answer to "sync or async" — this is the one that
|
|
107
|
+
cannot simply await, not a different contract.
|
|
108
108
|
"""
|
|
109
109
|
|
|
110
110
|
def redact(value: str) -> Any:
|
|
111
|
-
|
|
112
|
-
if inspect.isawaitable(result):
|
|
113
|
-
return asyncio.run(_resolved(result))
|
|
114
|
-
return result
|
|
111
|
+
return drive_hook_result(redact_fn(value), "redact_sensitive_information")
|
|
115
112
|
|
|
116
113
|
return redact
|
|
117
114
|
|
|
@@ -27,7 +27,15 @@ from agentcat.modules.constants import (
|
|
|
27
27
|
#
|
|
28
28
|
# `event_tags`, `event_properties` and `resolve_session_id` all receive this same
|
|
29
29
|
# pair — see EventTagsFunction / EventPropertiesFunction / ResolveSessionIdFunction.
|
|
30
|
-
|
|
30
|
+
#
|
|
31
|
+
# Accepts sync or async callables (mirrors RedactionFunction). This alias was the
|
|
32
|
+
# last of the five to say so: `resolve_identity` used to call the hook and take
|
|
33
|
+
# its return value verbatim, so an `async def` hook silently produced an
|
|
34
|
+
# anonymous event. `modules/hooks.py` now defines that contract for all of them.
|
|
35
|
+
IdentifyFunction = Callable[
|
|
36
|
+
[Any, Any],
|
|
37
|
+
Optional["UserIdentity"] | Awaitable[Optional["UserIdentity"]],
|
|
38
|
+
]
|
|
31
39
|
# Type alias for redaction function
|
|
32
40
|
RedactionFunction = Callable[[str], str | Awaitable[str]]
|
|
33
41
|
# Type alias for event_tags callback — returns str:str map attached to every auto-captured event.
|
|
@@ -188,6 +196,10 @@ class AgentCatOptions:
|
|
|
188
196
|
enable_tracing: bool = True
|
|
189
197
|
enable_tool_call_context: bool = True
|
|
190
198
|
custom_context_description: str = DEFAULT_CONTEXT_DESCRIPTION
|
|
199
|
+
# Callback invoked on every auto-captured event to attribute it to an actor.
|
|
200
|
+
# May be sync or async. Receives the `(request, extra)` pair every other hook
|
|
201
|
+
# receives. If it raises or returns anything but a UserIdentity, the event
|
|
202
|
+
# publishes anonymously rather than failing the tool call.
|
|
191
203
|
identify: IdentifyFunction | None = None
|
|
192
204
|
redact_sensitive_information: RedactionFunction | None = None
|
|
193
205
|
exporters: dict[str, ExporterConfig] | None = None
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"""The agent handle over real Streamable HTTP (community FastMCP 3).
|
|
2
|
+
|
|
3
|
+
`enable_agent_tracking` is off by default, so every OTHER e2e module in this
|
|
4
|
+
tree runs with `agent_id` never injected. This module turns it on for its own
|
|
5
|
+
server — the fixture reads `AGENTCAT_OPTIONS_FACTORY` per module — and it is a
|
|
6
|
+
separate file rather than a flag flipped on a shared one because `agent_id` is
|
|
7
|
+
injected as REQUIRED, which changes the schema every sibling test calls against.
|
|
8
|
+
|
|
9
|
+
The strip needs no recorder here: the conftest's `add_todo(text: str)` is a
|
|
10
|
+
typed FastMCP tool, and community FastMCP — unlike both official tool managers
|
|
11
|
+
— raises on an argument the signature never declared. A handle that survived to
|
|
12
|
+
the tool body fails the call.
|
|
13
|
+
|
|
14
|
+
Every `fastmcp` import is inside a test body, as in the rest of this tree: a
|
|
15
|
+
module-scope import fails at COLLECTION on the no-fastmcp matrix legs, which no
|
|
16
|
+
conftest gate downstream of it can rescue.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import time
|
|
22
|
+
|
|
23
|
+
import pytest
|
|
24
|
+
|
|
25
|
+
from agentcat import AgentCatOptions
|
|
26
|
+
from agentcat.modules.constants import (
|
|
27
|
+
AGENT_ID_PARAM,
|
|
28
|
+
AGENTCAT_TAG_AGENT_ID,
|
|
29
|
+
AGENTCAT_TAG_AGENT_SOURCE,
|
|
30
|
+
AGENTCAT_TAG_SESSION_SOURCE,
|
|
31
|
+
MCP_INSTRUCTIONS_KEY,
|
|
32
|
+
SESSION_ID_PARAM,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
pytestmark = pytest.mark.e2e
|
|
36
|
+
|
|
37
|
+
MINT_BACK_HEADER = "[MCP INSTRUCTIONS]: session_id issued."
|
|
38
|
+
AGENT = "opus-4.80-1m|claude-code|k3n9x"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _agent_tracking_options() -> AgentCatOptions:
|
|
42
|
+
return AgentCatOptions(enable_tracing=True, enable_agent_tracking=True)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
AGENTCAT_OPTIONS_FACTORY = _agent_tracking_options
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _call_events(capture_queue):
|
|
49
|
+
return [e for e in capture_queue if e.event_type == "mcp:tools/call"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _text(result) -> str:
|
|
53
|
+
return "".join(c.text for c in result.content if hasattr(c, "text"))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@pytest.mark.asyncio
|
|
57
|
+
async def test_the_agent_handle_survives_the_wire(v3_http_server, capture_queue):
|
|
58
|
+
"""Listing with agent tracking on: the schema the agent is handed.
|
|
59
|
+
|
|
60
|
+
Property order is the contract (`modules/injection.py` §"Resulting property
|
|
61
|
+
order"), and `agent_id` is required where `session_id` is not — omission is
|
|
62
|
+
the minting signal for one and nothing for the other.
|
|
63
|
+
"""
|
|
64
|
+
from fastmcp import Client
|
|
65
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
66
|
+
|
|
67
|
+
url, _ = v3_http_server
|
|
68
|
+
async with Client(StreamableHttpTransport(url)) as client:
|
|
69
|
+
listed = await client.list_tools()
|
|
70
|
+
|
|
71
|
+
add = next(t for t in listed if t.name == "add_todo")
|
|
72
|
+
assert list(add.inputSchema["properties"])[-3:] == [
|
|
73
|
+
SESSION_ID_PARAM,
|
|
74
|
+
AGENT_ID_PARAM,
|
|
75
|
+
"context",
|
|
76
|
+
]
|
|
77
|
+
assert AGENT_ID_PARAM in add.inputSchema["required"]
|
|
78
|
+
assert SESSION_ID_PARAM not in add.inputSchema["required"]
|
|
79
|
+
assert MCP_INSTRUCTIONS_KEY in add.outputSchema["properties"]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@pytest.mark.asyncio
|
|
83
|
+
async def test_a_supplied_agent_handle_tags_the_event(v3_http_server, capture_queue):
|
|
84
|
+
"""The handle rides the event as a tag and never reaches the tool."""
|
|
85
|
+
from fastmcp import Client
|
|
86
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
87
|
+
|
|
88
|
+
url, _ = v3_http_server
|
|
89
|
+
async with Client(StreamableHttpTransport(url)) as client:
|
|
90
|
+
result = await client.call_tool(
|
|
91
|
+
"add_todo",
|
|
92
|
+
{"text": "with agent", AGENT_ID_PARAM: AGENT, "context": "why"},
|
|
93
|
+
)
|
|
94
|
+
# The typed tool raises on any argument but `text`, so a surviving
|
|
95
|
+
# handle fails here rather than passing silently.
|
|
96
|
+
text = _text(result)
|
|
97
|
+
assert MINT_BACK_HEADER in text
|
|
98
|
+
minted = text.split("session_id=")[1].split(" ")[0]
|
|
99
|
+
mirror = result.structured_content[MCP_INSTRUCTIONS_KEY]
|
|
100
|
+
assert mirror[SESSION_ID_PARAM] == minted
|
|
101
|
+
assert mirror[AGENT_ID_PARAM] == AGENT
|
|
102
|
+
|
|
103
|
+
time.sleep(0.5)
|
|
104
|
+
event = _call_events(capture_queue)[-1]
|
|
105
|
+
assert event.tags[AGENTCAT_TAG_AGENT_ID] == AGENT
|
|
106
|
+
assert event.tags[AGENTCAT_TAG_AGENT_SOURCE] == "supplied"
|
|
107
|
+
assert event.tags[AGENTCAT_TAG_SESSION_SOURCE] == "minted"
|
|
108
|
+
assert event.parameters["arguments"][AGENT_ID_PARAM] == AGENT
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@pytest.mark.asyncio
|
|
112
|
+
async def test_both_handles_echo_across_calls(v3_http_server, capture_queue):
|
|
113
|
+
"""The agent echoes session and agent handle together on the next call.
|
|
114
|
+
|
|
115
|
+
The two are independent: the session is confirmed rather than re-minted,
|
|
116
|
+
while `agent_id` is `supplied` on both calls — the server never issues one,
|
|
117
|
+
so it has no `minted` state to pass through.
|
|
118
|
+
"""
|
|
119
|
+
from fastmcp import Client
|
|
120
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
121
|
+
|
|
122
|
+
url, _ = v3_http_server
|
|
123
|
+
async with Client(StreamableHttpTransport(url)) as client:
|
|
124
|
+
first = await client.call_tool(
|
|
125
|
+
"add_todo", {"text": "one", AGENT_ID_PARAM: AGENT, "context": "start"}
|
|
126
|
+
)
|
|
127
|
+
minted = _text(first).split("session_id=")[1].split(" ")[0]
|
|
128
|
+
|
|
129
|
+
second = await client.call_tool(
|
|
130
|
+
"add_todo",
|
|
131
|
+
{"text": "two", SESSION_ID_PARAM: minted, AGENT_ID_PARAM: AGENT},
|
|
132
|
+
)
|
|
133
|
+
assert MINT_BACK_HEADER not in _text(second)
|
|
134
|
+
assert second.structured_content[MCP_INSTRUCTIONS_KEY][AGENT_ID_PARAM] == AGENT
|
|
135
|
+
|
|
136
|
+
time.sleep(0.5)
|
|
137
|
+
events = _call_events(capture_queue)[-2:]
|
|
138
|
+
assert [e.session_id for e in events] == [minted, minted]
|
|
139
|
+
assert [e.tags[AGENTCAT_TAG_SESSION_SOURCE] for e in events] == [
|
|
140
|
+
"minted",
|
|
141
|
+
"supplied",
|
|
142
|
+
]
|
|
143
|
+
assert [e.tags[AGENTCAT_TAG_AGENT_ID] for e in events] == [AGENT, AGENT]
|
|
144
|
+
assert [e.tags[AGENTCAT_TAG_AGENT_SOURCE] for e in events] == [
|
|
145
|
+
"supplied",
|
|
146
|
+
"supplied",
|
|
147
|
+
]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@pytest.mark.asyncio
|
|
151
|
+
async def test_omitting_the_required_agent_handle_degrades_to_absence(
|
|
152
|
+
v3_http_server, capture_queue
|
|
153
|
+
):
|
|
154
|
+
"""`required` is advisory: nothing enforces it, and nothing may break.
|
|
155
|
+
|
|
156
|
+
AgentCat strips the handles in middleware, before the tool's own argument
|
|
157
|
+
validation, so an agent that ignores the `required` marker is served
|
|
158
|
+
normally. The event is then simply agent-less: an absent handle must never
|
|
159
|
+
become an empty or invented tag, because a customer filtering on
|
|
160
|
+
`agentcat_agent_id` has to be able to tell "no agent told us" from "this
|
|
161
|
+
agent".
|
|
162
|
+
"""
|
|
163
|
+
from fastmcp import Client
|
|
164
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
165
|
+
|
|
166
|
+
url, _ = v3_http_server
|
|
167
|
+
async with Client(StreamableHttpTransport(url)) as client:
|
|
168
|
+
result = await client.call_tool("add_todo", {"text": "no agent"})
|
|
169
|
+
assert MINT_BACK_HEADER in _text(result)
|
|
170
|
+
assert AGENT_ID_PARAM not in result.structured_content[MCP_INSTRUCTIONS_KEY]
|
|
171
|
+
|
|
172
|
+
time.sleep(0.5)
|
|
173
|
+
event = _call_events(capture_queue)[-1]
|
|
174
|
+
assert AGENTCAT_TAG_AGENT_ID not in event.tags
|
|
175
|
+
assert AGENTCAT_TAG_AGENT_SOURCE not in event.tags
|
|
176
|
+
assert event.tags[AGENTCAT_TAG_SESSION_SOURCE] == "minted"
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@pytest.mark.asyncio
|
|
180
|
+
async def test_a_blank_agent_handle_is_a_miss_not_an_empty_tag(
|
|
181
|
+
v3_http_server, capture_queue
|
|
182
|
+
):
|
|
183
|
+
"""`extract_handle` trims and rejects, over the wire.
|
|
184
|
+
|
|
185
|
+
A whitespace-only value must leave the event with NO agent tags — an
|
|
186
|
+
`agentcat_agent_id: ""` would key every such call to one phantom agent
|
|
187
|
+
downstream, which is worse than the absence it stands in for.
|
|
188
|
+
"""
|
|
189
|
+
from fastmcp import Client
|
|
190
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
191
|
+
|
|
192
|
+
url, _ = v3_http_server
|
|
193
|
+
async with Client(StreamableHttpTransport(url)) as client:
|
|
194
|
+
await client.call_tool(
|
|
195
|
+
"add_todo", {"text": "blank", AGENT_ID_PARAM: " ", "context": "x"}
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
time.sleep(0.5)
|
|
199
|
+
event = _call_events(capture_queue)[-1]
|
|
200
|
+
assert AGENTCAT_TAG_AGENT_ID not in event.tags
|
|
201
|
+
assert AGENTCAT_TAG_AGENT_SOURCE not in event.tags
|
|
202
|
+
# The session handle is unaffected: suppression is per handle.
|
|
203
|
+
assert event.tags[AGENTCAT_TAG_SESSION_SOURCE] == "minted"
|
|
@@ -88,16 +88,65 @@ async def test_v3_event_duration_is_non_negative(v3_http_server, capture_queue):
|
|
|
88
88
|
|
|
89
89
|
@pytest.mark.asyncio
|
|
90
90
|
async def test_client_identity_reaches_the_event(v3_http_server, capture_queue):
|
|
91
|
-
"""
|
|
92
|
-
|
|
91
|
+
"""Name AND version reach the tools/call event over a real connection.
|
|
92
|
+
|
|
93
|
+
The handshake capture is the last rung of the identity ladder and the only
|
|
94
|
+
one this era can use. Asserted against a `client_info` this test supplies
|
|
95
|
+
rather than against "some name resolved": the SDK's own default satisfies a
|
|
96
|
+
truthiness check while proving nothing about what the ladder read, and
|
|
97
|
+
`client_version` is null-by-default, so a rung that dropped it would go
|
|
98
|
+
unnoticed by a name-only assertion.
|
|
99
|
+
"""
|
|
93
100
|
from fastmcp import Client
|
|
94
101
|
from fastmcp.client.transports import StreamableHttpTransport
|
|
102
|
+
from mcp.types import Implementation
|
|
95
103
|
|
|
96
104
|
url, _ = v3_http_server
|
|
97
|
-
async with Client(
|
|
105
|
+
async with Client(
|
|
106
|
+
StreamableHttpTransport(url),
|
|
107
|
+
client_info=Implementation(name="MyAgent", version="1.2.3"),
|
|
108
|
+
) as client:
|
|
98
109
|
await client.call_tool("add_todo", {"text": "who", "context": "x"})
|
|
99
110
|
|
|
100
111
|
time.sleep(0.5)
|
|
101
112
|
call_events = [e for e in capture_queue if e.event_type == "mcp:tools/call"]
|
|
102
113
|
assert call_events
|
|
103
|
-
|
|
114
|
+
event = call_events[-1]
|
|
115
|
+
assert (event.client_name, event.client_version) == ("MyAgent", "1.2.3")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@pytest.mark.asyncio
|
|
119
|
+
async def test_identity_rides_every_call_not_just_the_first(
|
|
120
|
+
v3_http_server, capture_queue
|
|
121
|
+
):
|
|
122
|
+
"""Name AND version on EVERY event of a connection.
|
|
123
|
+
|
|
124
|
+
Reading only the last event cannot tell "resolved per request" from
|
|
125
|
+
"resolved once and reused", and the two differ exactly where it matters: a
|
|
126
|
+
rung that answers only for the call following the handshake leaves every
|
|
127
|
+
later event of a long-lived connection anonymous. This is a STATEFUL app,
|
|
128
|
+
so the connection whose handshake was captured is still there for all three
|
|
129
|
+
calls — the rung has to answer more than once.
|
|
130
|
+
"""
|
|
131
|
+
from fastmcp import Client
|
|
132
|
+
from fastmcp.client.transports import StreamableHttpTransport
|
|
133
|
+
from mcp.types import Implementation
|
|
134
|
+
|
|
135
|
+
url, _ = v3_http_server
|
|
136
|
+
async with Client(
|
|
137
|
+
StreamableHttpTransport(url),
|
|
138
|
+
client_info=Implementation(name="Cursor", version="2.6.22"),
|
|
139
|
+
) as client:
|
|
140
|
+
for n in range(3):
|
|
141
|
+
await client.call_tool("add_todo", {"text": f"call-{n}", "context": "id"})
|
|
142
|
+
|
|
143
|
+
time.sleep(0.5)
|
|
144
|
+
events = [e for e in capture_queue if e.event_type == "mcp:tools/call"][-3:]
|
|
145
|
+
assert [e.parameters["arguments"]["text"] for e in events] == [
|
|
146
|
+
"call-0",
|
|
147
|
+
"call-1",
|
|
148
|
+
"call-2",
|
|
149
|
+
]
|
|
150
|
+
assert [(e.client_name, e.client_version) for e in events] == [
|
|
151
|
+
("Cursor", "2.6.22")
|
|
152
|
+
] * 3
|