-
Notifications
You must be signed in to change notification settings - Fork 299
[MCP] Centralize JSON‑RPC error handling, standardize MCP response envelope, and add default anonymous role for stdio mode. #3035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
mcp-stdio mode code changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR is a follow-up to #2983 that improves the MCP stdio server implementation by centralizing JSON-RPC error handling, standardizing response writing, and adding an implicit default role when none is provided.
- Centralized all JSON-RPC error codes into a single
JsonRpcErrorCodesclass - Standardized response handling through shared
WriteResultandWriteErrorhelper methods - Added implicit default role of "anonymous" when
--mcp-stdiois used without a role parameter
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Service/Utilities/McpStdioHelper.cs | Added [NotNullWhen(true)] attribute to enforce non-null role guarantee, implemented implicit default role of "anonymous", and removed unnecessary service scope creation since services are singletons |
| src/Service/Program.cs | Added copyright header (formatting artifact in diff) |
| src/Azure.DataApiBuilder.Mcp/Core/JsonRpcErrorCodes.cs | New file defining centralized JSON-RPC 2.0 error codes for consistent error handling across the MCP server |
| src/Azure.DataApiBuilder.Mcp/Core/McpStdioServer.cs | Replaced hardcoded error codes with constants from JsonRpcErrorCodes, refactored initialize handler to use standardized WriteResult, and updated server name in initialization response |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Aniruddh25
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, but please fix the title to mention "what" the code changes are. Follow up PR doesnt convey much information unless you look into the changes. The PR titles are seen directly in the Release notes, so they should be more meaningful. For example, in this case, it is refactor to use common JsonRpcErrorCodes
mcp-stdio mode code changes.|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
souvikghosh04
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments for appropriate way of naming and versioning of MCP server and some naming/structuring conventions.
| /// These values come from the JSON-RPC 2.0 specification and are shared | ||
| /// so they are not hard-coded throughout the codebase. | ||
| /// </summary> | ||
| internal static class JsonRpcErrorCodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please consider renaming this to something like McpStdioJsonRpcErrorCodes or something to reflect its related to only MCP. also, we should move this inside "../src/Azure.DataApiBuilder.Mcp/Model" where we already have other error codes and enums, specifically for MCP.
| /// <summary> | ||
| /// The method does not exist / is not available. | ||
| /// </summary> | ||
| public const int METHODNOTFOUND = -32601; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please consider using existing format "SCREAMING_SNAKE_CASE or UPPER_SNAKE_CASE" for all constant variables. refer- ../src/Config/HealthCheck/HealthCheckConstants.cs
| name = "SQL MCP Server", | ||
| version = "1.0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please also update the name and version in ../src/Azure.DataApiBuilder.Mcp/Core/McpServerConfiguration.cs? for consistency, try to refactor it and use the same name and version through a configurable value or use the DAB version as the version will keep changing.
tagging @Aniruddh25 to confirm if we can use the DAB version itself or have a separate versioning for MCP server.
| /// <param name="mcpRole"> The role for MCP stdio mode, if specified.</param> | ||
| /// <returns></returns> | ||
| public static bool ShouldRunMcpStdio(string[] args, out string? mcpRole) | ||
| /// <param name="mcpRole"> The role for MCP stdio mode. When this method returns true, the role is guaranteed to be non-null.</param> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should mention mcpRole defaults to anonymous
| // Ensure that when MCP stdio is enabled, mcpRole is always non-null. | ||
| // This matches the NotNullWhen(true) contract and avoids nullable warnings | ||
| // for callers while still allowing an implicit default when no role is provided. | ||
| mcpRole ??= "anonymous"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this necessary? can we instead just initialize as mcpRole = "anonymous"; instead of mcpRole = null; in the beginning at line: 26?
Why make this change?
This is a follow up PR for, #2983 to address some of the newer comments.
What is this change?
anonymouswhen no role is provided inmcp-stdiomode.How was this tested?
Tested manually by calling all the CRUD tools and execute tool.
Added an MCP server in VS Code in
C:\DAB\data-api-builder\.vscode\mcp.json"dab-with-exe": { "command": "C:\\DAB\\data-api-builder\\out\\publish\\Debug\\net8.0\\win-x64\\dab\\Microsoft.DataApiBuilder.exe", "args": ["start", "--mcp-stdio", "--config", "C:\\DAB\\data-api-builder\\dab-config.json"], "env": { "DAB_ENVIRONMENT": "Development" } }Authenticatedall tools pass.Anonymous, all tools exceptdescribe-entitiesfail with permission denied error.describe-entitiesfail with permission denied error.