microsoft/semantic-kernel · error · ArgumentException
Resource uri is required.
Error message
Resource uri is required.
What it means
The ReadResource handler throws ArgumentException when GetResourceRequestParams.Uri is null or empty. The `is not string { } resourceUri` pattern requires a non-null string, then string.IsNullOrEmpty guards empties. This validates the client request before resource/template lookup.
Source
Thrown at dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs:224
IEnumerable<PromptDefinition> promptDefinitions = context.Server.Services!.GetServices<PromptDefinition>();
// Look up the prompt definition
PromptDefinition? definition = promptDefinitions.FirstOrDefault(d => d.Prompt.Name == promptName);
if (definition is null)
{
throw new ArgumentException($"No handler found for the prompt '{promptName}'.");
}
// Invoke the handler
return await definition.Handler(context, cancellationToken);
}
private static ValueTask<ReadResourceResult> HandleReadResourceRequestAsync(RequestContext<ReadResourceRequestParams> context, CancellationToken cancellationToken)
{
// Make sure the uri of the resource or resource template is provided
if (context.Params?.Uri is not string { } resourceUri || string.IsNullOrEmpty(resourceUri))
{
throw new ArgumentException("Resource uri is required.");
}
// Look up in registered resource first
IEnumerable<ResourceDefinition> resourceDefinitions = context.Server.Services!.GetServices<ResourceDefinition>();
ResourceDefinition? resourceDefinition = resourceDefinitions.FirstOrDefault(d => d.Resource.Uri == resourceUri);
if (resourceDefinition is not null)
{
return resourceDefinition.InvokeHandlerAsync(context, cancellationToken);
}
// Look up in registered resource templates
IEnumerable<ResourceTemplateDefinition> resourceTemplateDefinitions = context.Server.Services!.GetServices<ResourceTemplateDefinition>();
foreach (var resourceTemplateDefinition in resourceTemplateDefinitions)
{
if (resourceTemplateDefinition.IsMatch(resourceUri))
{View on GitHub (pinned to c028a0c7dc)
Solutions
- Ensure the client always sends a non-empty `uri` in resources/read params.
- Fix or upgrade the client SDK.
- Pre-validate uris client-side before issuing the read.
- On the server, translate this into a structured MCP error response.
Example fix
// before
if (context.Params?.Uri is not string { } resourceUri || string.IsNullOrEmpty(resourceUri))
{
throw new ArgumentException("Resource uri is required.");
}
// after (return MCP error response)
if (string.IsNullOrWhiteSpace(context.Params?.Uri))
{
return new ValueTask<ReadResourceResult>(
Task.FromResult(new ReadResourceResult { /* IsError = true */ }));
} Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(context.Params?.Uri))
return BadRequestError("resources/read requires a non-empty 'uri'."); Type guard
static bool HasResourceUri(ReadResourceRequestParams? p) =>
!string.IsNullOrEmpty(p?.Uri); Try / catch
try { return await HandleReadResourceRequestAsync(context, ct); }
catch (ArgumentException ex) when (ex.Message.Contains("Resource uri is required"))
{ return McpError(-32602, ex.Message); } Prevention
- Always send a non-empty uri in resources/read.
- Return structured MCP errors instead of throwing on the server.
- Validate the uri client-side first.
When it happens
Trigger: context.Params is null, or context.Params.Uri is null or empty string — a malformed resources/read request.
Common situations: Custom/buggy MCP client omits the uri field, an SDK serialization issue, or a hand-crafted JSON-RPC call missing uri.
Related errors
- Prompt name is required.
- No handler found for the resource uri '{resourceUri}'.
- No handler found for the prompt '{promptName}'.
- Resource constraint is not set. Do not try to call this meth
- Invalid choice
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/cd8f568c97094369.
Report an issue: GitHub.