Reserved URI-template expansion throws for supplementary Unicode characters
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
Research direction
Start with the reserved-expansion encoder in src/ModelContextProtocol.Core/UriTemplate.cs, around the Encode and AppendHex functions (about line 467-487), where the new Rune(char) call fails on surrogate pairs. Trace how ReadResourceAsync in McpClient.Methods.cs reaches FormatUri. Done when {+name} and {#name} with a supplementary character such as U+1F600 produce UTF-8 percent-encoded output and the read succeeds, with a regression test added to McpClientResourceTemplateTests.
Written by the indexing model from the issue text.
Description
Description
McpClient.ReadResourceAsync(uriTemplate, arguments) throws ArgumentOutOfRangeException when a reserved ({+var}) or fragment ({#var}) URI-template expansion contains a supplementary Unicode character, such as an emoji. This prevents a client from reading resources whose template arguments include those characters, before a resources/read request is sent.
Reproduction
On main at 3338e88e15c42cfc27465143c140d7d10f1a2707, with a connected client and a resource handler that echoes the requested URI:
var result = await client.ReadResourceAsync(
"test://resource/{+name}",
new Dictionary<string, object?> { ["name"] = "hello\U0001F600" });
Expected: The request URI is test://resource/hello%F0%9F%98%80, with U+1F600 percent-encoded as UTF-8, and the resource can be read.
Actual: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'ch'). The focused regression test fails on net10.0 with this stack:
at System.Text.Rune..ctor(Char ch)
at ModelContextProtocol.UriTemplate.<Encode>g__AppendHex|6_0(...) in UriTemplate.cs:487
at ModelContextProtocol.UriTemplate.Encode(String value, Boolean allowReserved) in UriTemplate.cs:467
at ModelContextProtocol.UriTemplate.FormatUri(...) in UriTemplate.cs:392
at ModelContextProtocol.Client.McpClient.ReadResourceAsync(...) in McpClient.Methods.cs:596
I reproduced this by adding a [Fact] to McpClientResourceTemplateTests using its in-memory client/server fixture, then running:
dotnet test tests/ModelContextProtocol.Tests/ModelContextProtocol.Tests.csproj -f net10.0 --filter "FullyQualifiedName~ReservedExpansion_EncodesSupplementaryUnicodeScalar"
Result: 1 failed, 0 passed. The client/server connection completed; expansion failed before the read request.
Cause and possible fix
The reserved-expansion encoder loops over UTF-16 char values and calls new Rune(c) for each non-ASCII character. A supplementary Unicode scalar is a surrogate pair, so constructing a Rune from its first char throws. Decode the scalar from the input span (and advance past both UTF-16 code units) before writing its UTF-8 percent-encoded bytes. A regression test could cover {+name} and {#name} with supplementary Unicode characters.
Relevant code: UriTemplate.cs:487, McpClient.Methods.cs:596. RFC 6570 specifies UTF-8 percent encoding for characters outside the allowed URI set.
- Dominant language
- C#
- Stars
- 4.6k
- Forks
- 849
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 24
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from modelcontextprotocol/csharp-sdk
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
modelcontextprotocol/csharp-sdk#2031 ·
Maintainers usually reply within 1 day
-
Static resources whose URIs differ only in the fragment or user info collapse into onePossibly taken @halter73 claimed this 10 days ago. OpenP2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xxPossibly taken @halter73 claimed this 10 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
modelcontextprotocol/csharp-sdk#1966 · 5 comments · 1 assignee ·
Maintainers usually reply within 1 day
-
ClaimsPrincipal parameter injection sample requestPossibly taken @utsavjain2809 claimed this 48 days ago. Openhelp wanted P3
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
modelcontextprotocol/csharp-sdk#1899 ·
Maintainers usually reply within 1 day
-
Document ClientOAuthOptions.ScopeSelector usagePossibly taken @z0rimo claimed this 71 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
modelcontextprotocol/csharp-sdk#1867 · 1 comment ·
Maintainers usually reply within 1 day
All issues in modelcontextprotocol/csharp-sdk
Similar issues
-
CLI: stale build hint, non-gofmt autocomplete output and a leftover `--revit=<year>` in the docsOpenCommand Line Utility (CLI)
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pyrevitlabs/pyRevit#4217 ·
Maintainers usually reply within 1 day
-
area: ui bug ready
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 1 day
-
account bug effort:S P2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nightscout/nocturne#3745 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
grate-devs/grate#969 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jsboige/CoursIA#27768 · 2 comments ·
Maintainers usually reply within 1 day