Skip to content

Conversation

@anushakolan
Copy link
Contributor

@anushakolan anushakolan commented Dec 30, 2025

Why make this change?

This is a follow up PR for, #2983 to address some of the newer comments.

What is this change?

  1. Centralized JSON‑RPC error handling: all standard JSON‑RPC error codes are now defined in one place and used consistently throughout the MCP server (for things like parse errors, invalid requests, unknown methods, invalid parameters, and internal errors).
  2. Standardized how responses are written: both successful results and errors now go through shared helpers, so every MCP response has a consistent JSON‑RPC envelope and is easy to change or audit.
  3. Added an implicit default role to be anonymous when no role is provided in mcp-stdio mode.

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" } }

  1. When calling tools with Authenticated all tools pass.
  2. When calling tools with Anonymous, all tools except describe-entities fail with permission denied error.
  3. When calling tools without any role parameter, all tools except describe-entities fail with permission denied error.

@anushakolan
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@anushakolan anushakolan marked this pull request as ready for review December 30, 2025 01:43
Copilot AI review requested due to automatic review settings December 30, 2025 01:43
@anushakolan anushakolan changed the title Addressed comments Follow up PR for mcp-stdio mode code changes. Dec 30, 2025
Copy link
Contributor

Copilot AI left a 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 JsonRpcErrorCodes class
  • Standardized response handling through shared WriteResult and WriteError helper methods
  • Added implicit default role of "anonymous" when --mcp-stdio is 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.

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a 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

@anushakolan anushakolan changed the title Follow up PR for mcp-stdio mode code changes. Centralize JSON‑RPC error handling, standardize MCP response envelope, and add default anonymous role for stdio mode. Dec 30, 2025
@anushakolan anushakolan changed the title Centralize JSON‑RPC error handling, standardize MCP response envelope, and add default anonymous role for stdio mode. [MCP] Centralize JSON‑RPC error handling, standardize MCP response envelope, and add default anonymous role for stdio mode. Dec 30, 2025
@anushakolan
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@anushakolan
Copy link
Contributor Author

/azp run

@anushakolan anushakolan enabled auto-merge (squash) January 14, 2026 19:06
@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@Aniruddh25
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@anushakolan
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

Copy link
Contributor

@souvikghosh04 souvikghosh04 left a 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
Copy link
Contributor

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;
Copy link
Contributor

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

Comment on lines +171 to +172
name = "SQL MCP Server",
version = "1.0.0"
Copy link
Contributor

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>
Copy link
Contributor

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";
Copy link
Contributor

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?

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.

4 participants