{"record":{"id":"cd8f568c97094369","repo":"microsoft/semantic-kernel","slug":"resource-uri-is-required","errorCode":null,"errorMessage":"Resource uri is required.","messagePattern":"Resource uri is required\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs","lineNumber":224,"sourceCode":"        IEnumerable<PromptDefinition> promptDefinitions = context.Server.Services!.GetServices<PromptDefinition>();\n\n        // Look up the prompt definition\n        PromptDefinition? definition = promptDefinitions.FirstOrDefault(d => d.Prompt.Name == promptName);\n        if (definition is null)\n        {\n            throw new ArgumentException($\"No handler found for the prompt '{promptName}'.\");\n        }\n\n        // Invoke the handler\n        return await definition.Handler(context, cancellationToken);\n    }\n\n    private static ValueTask<ReadResourceResult> HandleReadResourceRequestAsync(RequestContext<ReadResourceRequestParams> context, CancellationToken cancellationToken)\n    {\n        // Make sure the uri of the resource or resource template is provided\n        if (context.Params?.Uri is not string { } resourceUri || string.IsNullOrEmpty(resourceUri))\n        {\n            throw new ArgumentException(\"Resource uri is required.\");\n        }\n\n        // Look up in registered resource first\n        IEnumerable<ResourceDefinition> resourceDefinitions = context.Server.Services!.GetServices<ResourceDefinition>();\n\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            {","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/McpServerBuilderExtensions.cs#L206-L242","documentation":"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.","triggerScenarios":"context.Params is null, or context.Params.Uri is null or empty string — a malformed resources/read request.","commonSituations":"Custom/buggy MCP client omits the uri field, an SDK serialization issue, or a hand-crafted JSON-RPC call missing uri.","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."],"exampleFix":"// before\nif (context.Params?.Uri is not string { } resourceUri || string.IsNullOrEmpty(resourceUri))\n{\n    throw new ArgumentException(\"Resource uri is required.\");\n}\n\n// after (return MCP error response)\nif (string.IsNullOrWhiteSpace(context.Params?.Uri))\n{\n    return new ValueTask<ReadResourceResult>(\n        Task.FromResult(new ReadResourceResult { /* IsError = true */ }));\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(context.Params?.Uri))\n    return BadRequestError(\"resources/read requires a non-empty 'uri'.\");","typeGuard":"static bool HasResourceUri(ReadResourceRequestParams? p) =>\n    !string.IsNullOrEmpty(p?.Uri);","tryCatchPattern":"try { return await HandleReadResourceRequestAsync(context, ct); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Resource uri is required\"))\n{ return McpError(-32602, ex.Message); }","preventionTips":["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."],"tags":["mcp","validation","server","resources"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}