+
Skip to content

Conversation

nightscape
Copy link
Contributor

@nightscape nightscape commented Jul 1, 2025

This PR adds debugging functionality to Metals' MCP
integration, enabling AI tools to interact with and monitor Scala debug sessions.

New MCP debugging tools

Launching, listing, stopping debugging sessions

  • debug-main - Start a debug session for a main class with optional arguments, environment variables, and initial breakpoints
  • debug-test - Start a debug session for a test suite with optional test method filtering and initial breakpoints
  • debug-attach - Attach to a remote JVM debug port (default 5005) with hostname and module specification
  • debug-sessions - List active debug sessions, optionally including historical sessions from past runs
  • debug-terminate - Terminate a debug session and clean up resources

Inspecting

  • debug-threads - List all threads in a debug session with their current states
  • debug-stack-trace - Get the call stack for a specific thread showing method names and source locations
  • debug-evaluate - Evaluate expressions in the current debug context and return results with type information
  • debug-variables - Get all variables in the current debug scope, organized by scope name
  • debug-output - Retrieve stdout/stderr output from debug sessions with filtering by type, regex patterns, and line limits

Stepping

  • debug-pause - Pause execution in a specific debug session and thread
  • debug-continue - Continue execution in a paused debug session
  • debug-step - Step execution (in/out/over) for a specific thread in a debug session
  • debug-breakpoints - Set or clear breakpoints in source files for a debug session

Implementation

  • MetalsMcpServer acts like a DAP client (connects to a socket and sends/receives DAP messages)
  • DebugOutputManager stores and retrieves current and historical debug session outputs with in-memory caching and optional disk persistence
  • DebugStateManager - tracks session state and events

@nightscape nightscape force-pushed the mcp-debug branch 4 times, most recently from bc6b704 to 5b76d63 Compare July 2, 2025 10:49
@tgodzik
Copy link
Contributor

tgodzik commented Jul 2, 2025

This is a pretty big feature, nice work! Were you able to actually get an agent to use the workflow?

@nightscape nightscape force-pushed the mcp-debug branch 2 times, most recently from 91bb0a7 to 9ac0354 Compare July 3, 2025 08:14
@nightscape
Copy link
Contributor Author

@tgodzik I changed the implementation approach and the PR is smaller now.
I'm still debugging some issues with session creation.
I had Claude use the debugging tools, but there were still too many bugs, so the overall debugging sessions was not a success yet 😉
I'm hoping that changes quickly once the bugs are ironed out.

@nightscape nightscape force-pushed the mcp-debug branch 10 times, most recently from 1606db7 to a6910d3 Compare July 10, 2025 11:58
@nightscape nightscape force-pushed the mcp-debug branch 8 times, most recently from 469af24 to 7c4d2dc Compare July 17, 2025 08:12
@nightscape nightscape force-pushed the mcp-debug branch 3 times, most recently from 12965e3 to e85732d Compare July 17, 2025 23:56
@nightscape nightscape force-pushed the mcp-debug branch 2 times, most recently from f5f2fad to 47d7ef4 Compare July 18, 2025 07:41
@nightscape
Copy link
Contributor Author

@tgodzik the feature is becoming usable now, feel free to give it a try!

// for JSON formatted doctor
"com.lihaoyi" %% "ujson" % "4.2.1",
// For MCP communication
"com.github.andyglow" %% "scala-jsonschema-circe-json" % "0.7.11",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Under which conditions is it ok to introduce a new dependency?
The MetalsMcpServer became quite large with the set of new tools, so I used this library to generate the JSON Schema for the tools and did a few other refactorings to reduce the amount of boilerplate.

Copy link
Contributor

Choose a reason for hiding this comment

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

We usually try to minimize the amount of dependencies, so if it's not 100% needed we don't add it. I would rather avoid it if possible. We can easily separate the tools into traits and then just have one class to create them all

@nightscape nightscape force-pushed the mcp-debug branch 3 times, most recently from 441234c to f723d42 Compare July 18, 2025 23:56
This refactor eliminates code duplication in the MCP (Model Context Protocol) server
implementation by introducing shared type definitions and helper methods.

Summary

- Added circe JSON schema dependency - Introduces scala-jsonschema-circe-json for
automatic schema generation
- Created reusable type-safe tool creation helper - New createAsyncTool[T] method with
automatic schema generation from case classes
- Introduced trait-based parameter validation - HasOptionalFileInFocus, HasFileInFocus,
and HasFqcn traits for consistent parameter handling
- Replaced manual JSON schema strings - All MCP tools now use generated schemas from case
class definitions
- Added utility for Java object to Circe JSON conversion - javaObjectToJson helper method

Key Changes

1. Type Safety: Each MCP tool now has a corresponding case class with proper Circe
decoders and JSON schema generation
2. Code Reduction: Eliminated ~500 lines of duplicate schema definitions and argument
parsing code
3. Consistency: All tools now follow the same pattern for parameter validation and error
handling
4. Maintainability: Schema changes only require updating case class definitions instead of
  manual JSON strings

Technical Details

- Uses com.github.andyglow.jsonschema.AsCirce for automatic schema generation
- Implements proper type-safe parameter extraction with compile-time validation
- Maintains backward compatibility with existing MCP tool contracts
- Improves error handling with consistent error message formatting
- `fileInFocus` (string, optional): Current file for context

#### typed-glob-search
Searches for symbols by their type signatures.
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, this is not like hoogle or inquire, which might also be useful. The type here is whether it's a method/object etc.

// for JSON formatted doctor
"com.lihaoyi" %% "ujson" % "4.2.1",
// For MCP communication
"com.github.andyglow" %% "scala-jsonschema-circe-json" % "0.7.11",
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually try to minimize the amount of dependencies, so if it's not 100% needed we don't add it. I would rather avoid it if possible. We can easily separate the tools into traits and then just have one class to create them all

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.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载