microsoft/semantic-kernel · error · ArgumentException
No handler found for the resource uri '{resourceUri}'.
Error message
No handler found for the resource uri '{resourceUri}'. What it means
After validating the uri, the handler checks registered ResourceDefinitions (exact Uri match), then iterates ResourceTemplateDefinitions calling IsMatch. If neither matches, it throws ArgumentException naming the missing uri. This is the 'uri is well-formed but unknown to this server' terminal branch.
Source
Thrown at dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs:247
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))
{
return resourceTemplateDefinition.InvokeHandlerAsync(context, cancellationToken);
}
}
throw new ArgumentException($"No handler found for the resource uri '{resourceUri}'.");
}
private static ValueTask<ListResourceTemplatesResult> HandleListResourceTemplatesRequestAsync(RequestContext<ListResourceTemplatesRequestParams> context, CancellationToken cancellationToken)
{
// Get and return all resource template definitions registered in the DI container
IEnumerable<ResourceTemplateDefinition> definitions = context.Server.Services!.GetServices<ResourceTemplateDefinition>();
return ValueTask.FromResult(new ListResourceTemplatesResult
{
ResourceTemplates = [.. definitions.Select(d => d.ResourceTemplate)]
});
}
private static ValueTask<ListResourcesResult> HandleListResourcesRequestAsync(RequestContext<ListResourcesRequestParams> context, CancellationToken cancellationToken)
{
// Get and return all resource template definitions registered in the DI container
IEnumerable<ResourceDefinition> definitions = context.Server.Services!.GetServices<ResourceDefinition>();
View on GitHub (pinned to c028a0c7dc)
Solutions
- Call resources/list and resources/templates/list to discover valid uris/templates.
- Confirm the server registers the expected ResourceDefinition/ResourceTemplateDefinition at startup.
- Inspect IsMatch patterns for the templates to ensure they cover the requested uri shape.
- Return a structured MCP not-found error rather than throwing.
Example fix
// before
foreach (var t in resourceTemplateDefinitions)
{
if (t.IsMatch(resourceUri)) return t.InvokeHandlerAsync(context, cancellationToken);
}
throw new ArgumentException($"No handler found for the resource uri '{resourceUri}'.");
// after (log available candidates, structured error)
var matched = resourceTemplateDefinitions.FirstOrDefault(t => t.IsMatch(resourceUri));
if (matched is null)
{
logger.LogWarning("Unknown resource uri {Uri}. Known: {Known}", resourceUri,
string.Join(", ", resourceDefinitions.Select(d => d.Resource.Uri)));
return new ValueTask<ReadResourceResult>(
Task.FromResult(new ReadResourceResult { /* IsError = true */ }));
}
return matched.InvokeHandlerAsync(context, cancellationToken); Defensive patterns
Strategy: try-catch
Validate before calling
var knownResources = resourceDefinitions.Select(d => d.Resource.Uri).ToList();
bool templateMatches = resourceTemplateDefinitions.Any(t => t.IsMatch(resourceUri));
if (!knownResources.Contains(resourceUri) && !templateMatches)
return NotFoundError($"Unknown resource uri '{resourceUri}'."); Type guard
static bool ResourceResolves(IEnumerable<ResourceDefinition> defs,
IEnumerable<ResourceTemplateDefinition> templates, string uri) =>
defs.Any(d => d.Resource.Uri == uri) || templates.Any(t => t.IsMatch(uri)); Try / catch
try { return await resourceTemplateDefinition.InvokeHandlerAsync(context, ct); }
catch (ArgumentException ex) when (ex.Message.Contains("No handler found for the resource uri"))
{ return McpError(-32602, ex.Message); } Prevention
- Call resources/list and resources/templates/list to discover valid uris.
- Verify template IsMatch patterns cover expected uri shapes.
- Register expected resources/templates at server startup.
When it happens
Trigger: resourceUri matches no registered ResourceDefinition.Uri and no ResourceTemplateDefinition.IsMatch returns true.
Common situations: Client requests a resource uri that isn't registered, a template pattern doesn't cover the requested uri, or template matching has a bug (e.g. anchoring/escaping).
Related errors
- No handler found for the prompt '{promptName}'.
- Resource uri is required.
- Prompt name is required.
- Plugin creation failed for {pluginName}
- The client does not support sampling.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/35403a69e77c2216.
Report an issue: GitHub.