Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3053,11 +3053,11 @@ spec:
data included in audit logs (in bytes).
type: integer
maxDelegationDepth:
default: 10
default: 16
description: |-
MaxDelegationDepth caps how many nested RFC 8693 "act" entries are
recorded in an audit event's delegation chain. Deeper chains are
truncated (marked with truncated=true). Defaults to 10 when unset.
truncated (marked with truncated=true). Defaults to 16 when unset.
minimum: 1
type: integer
type: object
Expand Down Expand Up @@ -8908,11 +8908,11 @@ spec:
data included in audit logs (in bytes).
type: integer
maxDelegationDepth:
default: 10
default: 16
description: |-
MaxDelegationDepth caps how many nested RFC 8693 "act" entries are
recorded in an audit event's delegation chain. Deeper chains are
truncated (marked with truncated=true). Defaults to 10 when unset.
truncated (marked with truncated=true). Defaults to 16 when unset.
minimum: 1
type: integer
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3056,11 +3056,11 @@ spec:
data included in audit logs (in bytes).
type: integer
maxDelegationDepth:
default: 10
default: 16
description: |-
MaxDelegationDepth caps how many nested RFC 8693 "act" entries are
recorded in an audit event's delegation chain. Deeper chains are
truncated (marked with truncated=true). Defaults to 10 when unset.
truncated (marked with truncated=true). Defaults to 16 when unset.
minimum: 1
type: integer
type: object
Expand Down Expand Up @@ -8911,11 +8911,11 @@ spec:
data included in audit logs (in bytes).
type: integer
maxDelegationDepth:
default: 10
default: 16
description: |-
MaxDelegationDepth caps how many nested RFC 8693 "act" entries are
recorded in an audit event's delegation chain. Deeper chains are
truncated (marked with truncated=true). Defaults to 10 when unset.
truncated (marked with truncated=true). Defaults to 16 when unset.
minimum: 1
type: integer
type: object
Expand Down
73 changes: 64 additions & 9 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,20 @@ ToolHive installs no signal handler for this; the embedding program calls `Reope

#### Log Output Format

Audit events are logged as structured JSON objects:
Audit events are logged as structured JSON objects, one per line. Each line
the audit middleware and the composite-tool workflow auditor write also
carries `"level": "AUDIT"`, `"logger": "audit"`, `"msg": "audit_event"` and a
`time` key, which the example below leaves out; select those records in a log
pipeline with `logger=audit`. The lines are written by toolhive-core's audit
handler middleware (`audit.HandlerMiddleware`), unless an embedder routes the
events through its own logger (see
[Embedder-supplied logger](#embedder-supplied-logger) below).

The `obo_token_exchange` events of the On-Behalf-Of middleware and the vMCP
OBO auth strategy are written by a separate logger: they go to stdout with
`"level": "AUDIT"` but no `logger` key, are not routed to an embedder-supplied
logger, and carry no `metadata.extra.request_id`. Select them with
`level=AUDIT`.

```json
{
Expand All @@ -679,8 +692,7 @@ Audit events are logged as structured JSON objects:
"type": "network",
"value": "192.168.1.100",
"extra": {
"user_agent": "mcp-client/1.0",
"request_id": "req-12345"
"user_agent": "mcp-client/1.0"
}
},
"subjects": {
Expand Down Expand Up @@ -708,7 +720,8 @@ Audit events are logged as structured JSON objects:
"extra": {
"duration_ms": 150,
"transport": "streamable-http",
"response_size_bytes": 1024
"response_size_bytes": 1024,
"request_id": "0b6f6f0e-8f8e-4c1e-9a43-6a1f6f3c2d7b"
}
},
"data": {
Expand Down Expand Up @@ -736,8 +749,15 @@ Audit events are logged as structured JSON objects:
- `component`: Service/component that generated the event
- `source`: Information about the request source
- `type`: Source type (`network` for HTTP requests)
- `value`: Source identifier (client IP address)
- `extra`: Additional source metadata (user agent, request ID, etc.)
- `value`: Address of the connection the request arrived on (from the
connection's remote address). Behind a proxy or load balancer this is the
proxy's address. `X-Forwarded-For` and `X-Real-IP` are never read, because
a client can set them. An embedder that sets a request id (see
`metadata.extra.request_id` below) can find the original client by joining
it to the proxy's access log. `thv`, the proxy runner and the vMCP set no
request id, so their events carry no such join key
- `extra`: Additional source metadata (user agent). The client's
`X-Request-ID` header is never recorded
- `subjects`: Information about the authenticated user/client
- `user_id`: User subject identifier from JWT
- `user`: User display name (from `name` claim, `preferred_username`, or `email`)
Expand All @@ -760,9 +780,15 @@ Audit events are logged as structured JSON objects:
(data minimization per RFC 8693 §6); per OpenID Connect Core §5.7 the
(`iss`, `sub`) pair is the stable actor identifier
- `truncated`: `true` when the chain exceeded the configured maximum depth
(`maxDelegationDepth` in the audit config, default 10) and inner hops
were dropped; the outermost hops — including `chain[0]`, the current
actor — are always kept. Always present
(`maxDelegationDepth` in the audit config, default 16, toolhive-core's
default) and inner hops were dropped; the outermost hops — including
`chain[0]`, the current actor — are always kept. Always present. The
default is above ToolHive's own minting cap of 10 hops, so a recorded
chain longer than 10 hops was not minted by ToolHive. The CRD default was
10 before; the API server writes a CRD default into the object when it is
created or updated, so an existing `VirtualMCPServer` with an `audit`
block keeps a stored `maxDelegationDepth` of 10 until the field is edited
or removed
- `omitted`: Number of well-formed hops dropped due to the depth cap.
Always present; `0` when nothing was dropped
- `malformed`: `true` when the token's `act` claim violated RFC 8693
Expand All @@ -783,6 +809,13 @@ Audit events are logged as structured JSON objects:
- `duration_ms`: Request duration in milliseconds
- `transport`: Transport type (`sse`, `streamable-http`, `http`)
- `response_size_bytes`: Response body size (when capturing response data)
- `request_id`: The request id the request context carries, present only
when one was set. A program that embeds the audit middleware generates it
and stores it with toolhive-core's `audit.ContextWithRequestID` in a
middleware that wraps the audit middleware; an id set inside the audit
middleware is never seen. Composite-tool workflow and step events carry
the request id of the context they are logged with. ToolHive's own proxy
sets none, and a client-sent id is never used
- `trace_id`, `span_id`: W3C trace and span IDs of the request (present only when the request carries a valid span context, such as an inbound `traceparent` with tracing enabled)
- `denial_reason`: Why an inner check refused the request, when it names a reason (for example a vMCP call-binding refusal: `token_reused` or `token_expired`, answered with 401, or `arguments_mismatch`, answered with 403). A fixed string that never carries request data. Present only on denied events, including the `sse_connection` event of a refused stream-opening GET
- `admitted_after_expiry`: `true` when the token validator accepted the request's access token after it expired, as a message of a vMCP call that outlived its token. Present only on such events, whether the vMCP then admitted or refused the request, including the stream-open (`sse_connection`) event of a GET
Expand Down Expand Up @@ -832,6 +865,28 @@ cfg.AuditSubjectEnricher = func(ctx context.Context) map[string]string {
supply them.
- With no enricher set, events are unchanged.

#### Embedder-supplied logger

A program that embeds ToolHive can route the events of the audit middleware
and the composite-tool workflow auditor through its own `*slog.Logger`, for
example one whose handler is built with toolhive-core's
`audit.HandlerMiddleware`, so that ToolHive's audit events and its own reach
the same audit handler. Pass `audit.WithLogger(logger)` to
`audit.NewAuditorWithTransport` or `audit.NewWorkflowAuditor`, or, for the
vMCP, set `AuditLogger` on `server.Config`, or on both `server.ServerConfig`
and `core.Config` when it calls `core.New` and `server.Serve` directly.

- It is set from Go only. It has no key in the audit configuration file.
- Every event of those auditors, including the `sse_connection` event of a
stream and every composite-tool workflow and step event, is logged to it at
`audit.LevelAudit`. The `obo_token_exchange` events are not (see
[Log Output Format](#log-output-format)). The logger's handler decides where the event is written
and how it is marked: ToolHive adds no `logger=audit` key of its own.
- The auditor then opens no log writer, so closing it releases nothing.
- An audit configuration that sets `logFile` is rejected when a logger is
given, and no file is opened: the logger decides where events go.
- On the vMCP it has no effect when `AuditConfig` is nil.

#### CLI Usage

**With audit configuration file**:
Expand Down
2 changes: 1 addition & 1 deletion docs/operator/crd-api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/server/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/server/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/server/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading