Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 3, 2025

Overview

This PR implements structured logging via the Model Context Protocol (MCP) specification and distributed tracing via OpenTelemetry (OTEL) for sampling workflows, addressing issue #[feature].

What's Changed

🔔 MCP Logging Implementation

Implemented dual logging system that ensures visibility while supporting MCP clients:

New Features:

  • MCPLogger class that sends notifications/message events per MCP spec
  • Always logs to console for visibility during development and debugging
  • Additionally sends MCP notifications when server is available
  • Supports all MCP log levels: debug, info, notice, warning, error, critical, alert, emergency
  • Structured logging support (objects and strings)
  • Hierarchical logger names with child() method

Example Usage:

import { createLogger } from "./packages/core/src/utils/logger.ts";

const logger = createLogger("my-agent", server);
await logger.info("Processing started");
await logger.warning({ resource: "memory", usage: 85 });
await logger.error("Operation failed");

📊 OpenTelemetry Tracing

Implemented distributed tracing for sampling workflows with configurable export destinations:

Configuration via Environment Variables:

export MCPC_TRACING_ENABLED=true
export MCPC_TRACING_EXPORT=console  # or 'otlp' or 'none'
export MCPC_TRACING_OTLP_ENDPOINT=http://localhost:4318/v1/traces

Trace Hierarchy:

  • Root span for entire sampling loop
  • Child spans for each iteration
  • Automatic error tracking and exception recording
  • Compatible with Jaeger, Zipkin, Grafana Tempo, and other OTLP backends

🔧 Integration Points

Modified Components:

  • compose.ts: Server lifecycle events and tool matching warnings now use MCP logging
  • base-sampling-executor.ts: Iteration progress logging and automatic span creation for tracing
  • logging-plugin.ts: Composition logging now uses MCP notifications

Before:

console.log(`Iteration ${i}:`, data);
console.warn("Tool not found");

After:

await this.logger.debug({ iteration: i, data });
await this.logger.warning("Tool not found");

📦 Dependencies

Added OpenTelemetry packages to packages/core/deno.json:

  • @opentelemetry/api@^1.9.0
  • @opentelemetry/sdk-trace-base@^1.29.0
  • @opentelemetry/sdk-trace-node@^1.29.0
  • @opentelemetry/exporter-trace-otlp-http@^0.56.0
  • @opentelemetry/resources@^1.29.0
  • @opentelemetry/semantic-conventions@^1.29.0

📚 Documentation

Added comprehensive guide at docs/LOGGING_AND_TRACING.md covering:

  • MCP logging usage and best practices
  • OpenTelemetry configuration and setup
  • Integration with observability platforms
  • Code examples and patterns
  • Updated main README with documentation link

Backward Compatibility

✅ All changes are backward compatible:

  • Console logging is always enabled for visibility
  • MCP notifications are sent in addition to console logs when server is available
  • Tracing is opt-in via environment variables
  • No breaking changes to existing APIs
  • Examples continue to work unchanged

Testing

  • ✅ Logger verified with and without server (always logs to console)
  • ✅ Type checking passes for all modified files
  • ✅ Code formatted with deno fmt
  • ✅ Error handling for environment access in different runtimes

References

Fixes #12

Original prompt

This section details on the original issue you should resolve

<issue_title>[feature] implement logging, possibly OTEL tracing when sampling</issue_title>
<issue_description>https://modelcontextprotocol.io/legacy/tools/debugging#implementing-logging

https://github.com/open-telemetry/opentelemetry-specification</issue_description>

<agent_instructions>Please implement MCP logging as an alternative to current console logs

And in sampling workflow, implement both MCP logging and otel tracing </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@yaonyan https://modelcontextprotocol.io/specification/2025-03-26/server/utilities/logging
Fixes #12

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] [feature] implement logging, possibly OTEL tracing when sampling Implement MCP logging and OpenTelemetry tracing for sampling workflows Oct 3, 2025
Copilot AI requested a review from yaonyan October 3, 2025 07:41
Copilot finished work on behalf of yaonyan October 3, 2025 07:41
@yaonyan
Copy link
Contributor

yaonyan commented Oct 3, 2025

Make sure console logs are always enabled @copilot

Copy link
Contributor Author

Copilot AI commented Oct 3, 2025

Make sure console logs are always enabled @copilot

Done! Console logs are now always enabled. The logger now outputs to console regardless of whether an MCP server is available, and additionally sends MCP notifications when the server is present. See commit f46d250.

Copilot finished work on behalf of yaonyan October 3, 2025 07:57
…mpatibility

This commit introduces several improvements across the project:

*   **Documentation (`.github/copilot-instructions.md`, `docs/logging-and-tracing.md`):**
    *   Added a clear rule in Copilot instructions to avoid Deno-specific APIs, ensuring Node.js compatibility.
    *   Refactored and clarified the logging and tracing documentation for better readability and conciseness. Removed redundant information and updated feature descriptions.
*   **Tracing Configuration (`packages/copilot-sampled-executor/src/workflow/base-sampling-executor.ts`):**
    *   Updated OpenTelemetry tracing configuration to use `process.env` instead of `Deno.env`, aligning with the Node.js compatibility rule.
    *   Changed the default `exportTo` strategy from `console` to `otlp` and set a default OTLP endpoint, promoting standard tracing practices.
    *   Adjusted OpenTelemetry imports to leverage the Node.js SDK for better integration.
*   **Server Utilities (`packages/core/src/utils/server.ts`):**
    *   Removed Deno-specific environment checks and `TextEncoder` usage, further enforcing Node.js compatibility.
*   **Sampling Executor (`packages/copilot-sampled-executor/src/workflow/base-sampling-executor.ts`, `packages/copilot-sampling-agent/src/agent.ts`):**
    *   Added a `summarize` option to the `SamplerConfig` and integrated its passing from the agent to the sampling executor, allowing for control over summarization behavior.
@yaonyan yaonyan force-pushed the copilot/fix-957f615c-b304-4a70-b307-b48866d17a26 branch from 0a14147 to f748a51 Compare October 4, 2025 07:10
Copy link
Contributor

@yaonyan yaonyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified, then qualified

@yaonyan yaonyan marked this pull request as ready for review October 4, 2025 07:11
@yaonyan yaonyan merged commit 382ab7e into main Oct 4, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] implement logging, possibly OTEL tracing when sampling

2 participants