{"record":{"id":"35403a69e77c2216","repo":"microsoft/semantic-kernel","slug":"no-handler-found-for-the-resource-uri-resourceur","errorCode":null,"errorMessage":"No handler found for the resource uri '{resourceUri}'.","messagePattern":"No handler found for the resource uri '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs","lineNumber":247,"sourceCode":"\n        ResourceDefinition? resourceDefinition = resourceDefinitions.FirstOrDefault(d => d.Resource.Uri == resourceUri);\n        if (resourceDefinition is not null)\n        {\n            return resourceDefinition.InvokeHandlerAsync(context, cancellationToken);\n        }\n\n        // Look up in registered resource templates\n        IEnumerable<ResourceTemplateDefinition> resourceTemplateDefinitions = context.Server.Services!.GetServices<ResourceTemplateDefinition>();\n\n        foreach (var resourceTemplateDefinition in resourceTemplateDefinitions)\n        {\n            if (resourceTemplateDefinition.IsMatch(resourceUri))\n            {\n                return resourceTemplateDefinition.InvokeHandlerAsync(context, cancellationToken);\n            }\n        }\n\n        throw new ArgumentException($\"No handler found for the resource uri '{resourceUri}'.\");\n    }\n\n    private static ValueTask<ListResourceTemplatesResult> HandleListResourceTemplatesRequestAsync(RequestContext<ListResourceTemplatesRequestParams> context, CancellationToken cancellationToken)\n    {\n        // Get and return all resource template definitions registered in the DI container\n        IEnumerable<ResourceTemplateDefinition> definitions = context.Server.Services!.GetServices<ResourceTemplateDefinition>();\n\n        return ValueTask.FromResult(new ListResourceTemplatesResult\n        {\n            ResourceTemplates = [.. definitions.Select(d => d.ResourceTemplate)]\n        });\n    }\n\n    private static ValueTask<ListResourcesResult> HandleListResourcesRequestAsync(RequestContext<ListResourcesRequestParams> context, CancellationToken cancellationToken)\n    {\n        // Get and return all resource template definitions registered in the DI container\n        IEnumerable<ResourceDefinition> definitions = context.Server.Services!.GetServices<ResourceDefinition>();\n","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs#L229-L265","documentation":"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.","triggerScenarios":"resourceUri matches no registered ResourceDefinition.Uri and no ResourceTemplateDefinition.IsMatch returns true.","commonSituations":"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).","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."],"exampleFix":"// before\nforeach (var t in resourceTemplateDefinitions)\n{\n    if (t.IsMatch(resourceUri)) return t.InvokeHandlerAsync(context, cancellationToken);\n}\nthrow new ArgumentException($\"No handler found for the resource uri '{resourceUri}'.\");\n\n// after (log available candidates, structured error)\nvar matched = resourceTemplateDefinitions.FirstOrDefault(t => t.IsMatch(resourceUri));\nif (matched is null)\n{\n    logger.LogWarning(\"Unknown resource uri {Uri}. Known: {Known}\", resourceUri,\n        string.Join(\", \", resourceDefinitions.Select(d => d.Resource.Uri)));\n    return new ValueTask<ReadResourceResult>(\n        Task.FromResult(new ReadResourceResult { /* IsError = true */ }));\n}\nreturn matched.InvokeHandlerAsync(context, cancellationToken);","handlingStrategy":"try-catch","validationCode":"var knownResources = resourceDefinitions.Select(d => d.Resource.Uri).ToList();\nbool templateMatches = resourceTemplateDefinitions.Any(t => t.IsMatch(resourceUri));\nif (!knownResources.Contains(resourceUri) && !templateMatches)\n    return NotFoundError($\"Unknown resource uri '{resourceUri}'.\");","typeGuard":"static bool ResourceResolves(IEnumerable<ResourceDefinition> defs,\n    IEnumerable<ResourceTemplateDefinition> templates, string uri) =>\n    defs.Any(d => d.Resource.Uri == uri) || templates.Any(t => t.IsMatch(uri));","tryCatchPattern":"try { return await resourceTemplateDefinition.InvokeHandlerAsync(context, ct); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"No handler found for the resource uri\"))\n{ return McpError(-32602, ex.Message); }","preventionTips":["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."],"tags":["mcp","resources","lookup","server"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}