Skip to content

Tool invocation logging middleware#2340

Draft
mattdholloway wants to merge 1 commit into
mainfrom
draft/tool-logging-middleware
Draft

Tool invocation logging middleware#2340
mattdholloway wants to merge 1 commit into
mainfrom
draft/tool-logging-middleware

Conversation

@mattdholloway
Copy link
Copy Markdown
Contributor

Adds a sensible logging integration on top of the (currently unused) observability.Exporters scaffolding:

  • pkg/github/logging_middleware.go ToolLoggingMiddleware times every tools/call and logs at Debug on success / Error on failure (Go error or IsError result). It enriches a *slog.Logger with mcp.method and mcp.tool, then stores it on the context via observability.ContextWithLogger so tool handlers pick up the request-scoped logger automatically from deps.Logger(ctx).

  • pkg/observability/logger_context.go ContextWithLogger / LoggerFromContext helpers.

  • pkg/observability/log_level.go ParseLogLevel for 'debug|info|warn|error'.

  • --log-level flag / GITHUB_LOG_LEVEL env var Fills an obvious gap: previously levels were hard-coded (stderr=Info, file=Debug). The default behaviour is preserved when the flag is empty.

  • Fix fmt.Fprintf(os.Stderr, ...) feature-flag check reporting in BaseDeps.IsFeatureEnabled / RequestDeps.IsFeatureEnabled — these previously bypassed the structured logger entirely.

Tool handler code is intentionally untouched. The middleware gives every tool a duration/outcome log line uniformly; tools that want richer structured logs can opt in via deps.Logger(ctx).

Tests cover the middleware (success / error / IsError / non-tool pass-through / missing deps), the context helpers, level parsing, and the BaseDeps.Logger(ctx) context fallback.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Summary

Why

Fixes #

What changed

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

Prompts tested (tool changes only)

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Note: if you're renaming tools, you must add the tool aliases. For more information on how to do so, please refer to the official docs.

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed
  • Updated (README / docs / examples)
Adds a sensible logging integration on top of the (currently unused)
observability.Exporters scaffolding:

  * pkg/github/logging_middleware.go
    ToolLoggingMiddleware times every tools/call and logs at Debug on
    success / Error on failure (Go error or IsError result). It enriches
    a *slog.Logger with mcp.method and mcp.tool, then stores it on the
    context via observability.ContextWithLogger so tool handlers pick up
    the request-scoped logger automatically from deps.Logger(ctx).

  * pkg/observability/logger_context.go
    ContextWithLogger / LoggerFromContext helpers.

  * pkg/observability/log_level.go
    ParseLogLevel for 'debug|info|warn|error'.

  * --log-level flag / GITHUB_LOG_LEVEL env var
    Fills an obvious gap: previously levels were hard-coded (stderr=Info,
    file=Debug). The default behaviour is preserved when the flag is empty.

  * Fix fmt.Fprintf(os.Stderr, ...) feature-flag check reporting in
    BaseDeps.IsFeatureEnabled / RequestDeps.IsFeatureEnabled — these
    previously bypassed the structured logger entirely.

Tool handler code is intentionally untouched. The middleware gives every
tool a duration/outcome log line uniformly; tools that want richer
structured logs can opt in via deps.Logger(ctx).

Tests cover the middleware (success / error / IsError / non-tool
pass-through / missing deps), the context helpers, level parsing, and
the BaseDeps.Logger(ctx) context fallback.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mattdholloway mattdholloway force-pushed the draft/tool-logging-middleware branch from 1df340e to 9d3e8b7 Compare April 16, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants