Skip to content

AspNetCoreMcp: wrong-typed tool argument (JsonException from SDK marshalling) surfaces as kind "unknown" / internal error instead of a validation error #357

Open

Description

@duguankui

Summary

When a client passes a tool argument whose JSON type does not match the parameter type (for example an array for a string parameter), the SDK's argument marshalling throws a System.Text.Json.JsonException inside the call-tool pipeline. McpToolErrorFilter catches it, but classifies it as kind: "unknown" and, because ABP's IExceptionToErrorInfoConverter treats JsonException as an unexpected exception, the model only sees the generic "An internal error occurred" message. The one thing the model could act on - which argument has the wrong shape - is lost.

Observed with Dignite.Abp.AspNetCore.Mcp 10.7.0 (ModelContextProtocol.AspNetCore 2.1.0) in Dignite.Shiji.

Reproduction

Tool parameter declared as string? names = null; the client sends:

{"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"shiji_name_analyze","arguments":{"names":["杜冠魁","杜子涵"]}}}

Result handed to the model:

isError: true
text: 对不起,在处理您的请求期间产生了一个服务器内部错误!!
structuredContent.error.kind: "unknown"

Server log:

System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'StartArray' as a string.
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor.<>c__DisplayClass40_0.<GetParameterMarshaller>b__3(AIFunctionArguments arguments, CancellationToken _)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunction.InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)
   at ModelContextProtocol.Server.AIFunctionMcpServerTool.InvokeAsync(RequestContext`1 request, CancellationToken cancellationToken)
   at Dignite.Abp.AspNetCore.Mcp.Uow.AbpMcpUnitOfWorkFilter.RunAsync[TResult](...)
   at Dignite.Abp.AspNetCore.Mcp.Errors.McpToolErrorFilter.<>c__DisplayClass1_0.<<CallToolFilter>b__0>d.MoveNext()

Note that the exception is thrown by the SDK's AIFunctionMcpServerTool.InvokeAsync, i.e. by next(...) inside the filter, so it is not an McpProtocolException and is not rethrown as a JSON-RPC error either; it goes down the generic path.

Expected

The same contract a AbpValidationException gets: kind: "validation", a message that says the argument has the wrong type, and validationErrors[].fields naming the parameter, so the model can fix the one argument and retry in the same turn. Today a wrong-typed argument is indistinguishable from a crash in the tool body.

Proposed change (in McpToolErrorFilter)

  1. KindOf: add JsonException => McpToolErrorKinds.Validation (before the IBusinessException arm; JsonException implements none of the ABP interfaces, so placement only matters for readability).
  2. Describe: when the exception is a JsonException, do not go through the ABP converter (it hides the message by design). Build the error directly: message like "Argument has the wrong JSON type: <exception.Message>", and one McpToolValidationError whose Fields is the parameter name when it can be recovered. The SDK's marshaller exception carries Path: $ relative to the single argument value, not the parameter name, so the name has to come from elsewhere - either request.Params.Arguments (try to spot which argument's JSON kind disagrees with the tool's InputSchema property type, which the filter has through request.Server/the tool metadata), or, simplest and still useful, list all argument names whose JSON kind is an array/object while the schema says string/number/boolean.
  3. Log it at warning level like other validation failures (AbpExceptionHandlingOptions.ShouldLogException already applies; the level comes from LogException, which will treat it as an error today - consider wrapping it in an AbpValidationException instead, which gives the right kind, log level and shape in one step and avoids a second error-shaping path).

Option 3 (wrap into AbpValidationException with a ValidationResult whose MemberNames is the recovered argument name, then fall through to the existing path) is probably the smallest change that keeps the single error contract the class comment promises.

Also worth a note in docs

docs/en/aspnetcore-mcp says the filter turns every failure into a result a model can act on; the wrong-type case is the obvious exception today, and tool authors flattening parameters to primitives (the pattern that makes schemas portable across hosts) hit it as soon as a host sends an array for a comma-separated-string parameter.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmodule: aspnetcore-mcpaspnetcore-mcp/ (Dignite.Abp.AspNetCore.Mcp)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions