Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Reserved URI-template expansion throws for supplementary Unicode characters

未关闭 适合新手
#1,913 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 9 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
api

调研方向

从 src/ModelContextProtocol.Core/UriTemplate.cs 中的保留展开编码器开始,重点关注 Encode 和 AppendHex 函数(约第 467-487 行),其中 new Rune(char) 调用在代理对上失败。追踪 McpClient.Methods.cs 中的 ReadResourceAsync 如何到达 FormatUri。当 {+name} 和 {#name} 配合 U+1F600 等补充字符时能输出 UTF-8 百分号编码结果且读取成功,并已在 McpClientResourceTemplateTests 中添加回归测试,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C#
星标
4.6k
派生
819
平均合并
4 天 23 小时
30 天内合并 PR
2

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

modelcontextprotocol/csharp-sdk 的其他 Issue

查看 modelcontextprotocol/csharp-sdk 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。