Skip to content

增加 Sampling(委托生成)功能#7

Open
walterlv wants to merge 37 commits intomainfrom
t/lvyi/sampling
Open

增加 Sampling(委托生成)功能#7
walterlv wants to merge 37 commits intomainfrom
t/lvyi/sampling

Conversation

@walterlv
Copy link
Copy Markdown
Member

@walterlv walterlv commented Apr 7, 2026

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 为 DotNetCampus.ModelContextProtocol 增加 Sampling(服务器向客户端委托生成 / sampling/createMessage)能力,贯通能力协商、服务器侧发起请求、客户端侧处理并回包,以及在不同传输层(HTTP/SSE、STDIO 等)下的请求/响应路由。

Changes:

  • 新增服务器侧 Sampling API(IMcpServerSampling)与对应异常类型,并将 Sampling 注入到工具调用上下文中
  • 客户端侧新增 Sampling handler 配置(McpClientBuilder.WithSamplingHandler),并在传输层支持“服务器主动请求”的入站处理与回包
  • 更新 HTTP/STDIO 等传输层以区分并路由 JSON-RPC request vs response,并补充集成测试与 sample 工具演示

Reviewed changes

Copilot reviewed 34 out of 34 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/DotNetCampus.ModelContextProtocol.Tests/TestMcpFactory.cs 扩展测试工厂:允许额外配置客户端 builder 以支持 Sampling 测试场景
tests/DotNetCampus.ModelContextProtocol.Tests/Servers/SamplingTests.cs 新增 Sampling 端到端集成测试(服务端工具触发、客户端 handler 响应、能力检测)
tests/DotNetCampus.ModelContextProtocol.Tests/McpTools/SamplingTool.cs 新增测试用服务端工具:通过 context.Sampling 发起 sampling/createMessage
src/DotNetCampus.ModelContextProtocol/Transports/Stdio/StdioServerTransportSession.cs STDIO 会话支持服务器侧发起请求并等待响应(pending 请求表、写锁、响应路由)
src/DotNetCampus.ModelContextProtocol/Transports/Stdio/StdioServerTransport.cs STDIO server run loop 增加 response 检测与注入 Sampling/session 服务
src/DotNetCampus.ModelContextProtocol/Transports/Stdio/StdioClientTransport.cs STDIO client run loop 支持解析服务器发起的请求并转交客户端处理
src/DotNetCampus.ModelContextProtocol/Transports/ServerTransportManager.cs 统一使用内部 JSON 上下文;新增从内存解析 JSON-RPC response 的能力
src/DotNetCampus.ModelContextProtocol/Transports/IServerTransportSession.cs 扩展传输层会话:保存客户端能力、支持 SendRequest/HandleResponse
src/DotNetCampus.ModelContextProtocol/Transports/IServerTransportManager.cs 新增 ReadResponseAsync(ReadOnlyMemory<byte>) 供传输层路由 response
src/DotNetCampus.ModelContextProtocol/Transports/IClientTransportManager.cs 新增 HandleServerRequestAsync:处理服务器主动请求并回送响应
src/DotNetCampus.ModelContextProtocol/Transports/Http/LocalHostHttpServerTransportSession.cs HTTP session 增加服务器发起请求等待/响应路由(pending 请求表)
src/DotNetCampus.ModelContextProtocol/Transports/Http/LocalHostHttpServerTransport.cs POST body 支持区分 request/response;response 路由回 session;注入 transport session 服务
src/DotNetCampus.ModelContextProtocol/Transports/Http/HttpClientTransport.cs SSE message 分流:服务器请求进入 HandleServerRequestAsync,响应进入原响应处理链路
src/DotNetCampus.ModelContextProtocol/Transports/ClientTransportManager.cs 客户端传输管理器增加 Sampling handler 与服务器请求处理/回包逻辑
src/DotNetCampus.ModelContextProtocol/Servers/McpServerSampling.cs 新增服务器侧 Sampling 抽象与实现(通过 transport session 发起 sampling/createMessage)
src/DotNetCampus.ModelContextProtocol/Servers/McpServerRequestHandlers.cs Initialize 时将客户端 capabilities 写入当前 transport session,供 Sampling 支持检测
src/DotNetCampus.ModelContextProtocol/Servers/McpProtocolBridge.cs 切换到统一的内部 JSON 上下文(请求参数/响应结果)
src/DotNetCampus.ModelContextProtocol/Servers/IMcpServerPrimitiveContext.cs 工具上下文新增 Sampling 属性,并在实现中按服务解析/回退 NotSupported 实现
src/DotNetCampus.ModelContextProtocol/Protocol/Messages/Sampling.cs Sampling 消息类型调整:使用通用 ContentBlock 替代原 SamplingMessageContent
src/DotNetCampus.ModelContextProtocol/Protocol/Messages/Role.cs 枚举序列化属性调整为 JsonStringEnumMemberName
src/DotNetCampus.ModelContextProtocol/Protocol/Messages/ContentBlock.cs ContentBlock 多态 discriminator 更新(tool_use/tool_result)
src/DotNetCampus.ModelContextProtocol/Hosting/Services/McpServiceCollectionTransportExtensions.cs 新增扩展:统一注册 transport session + IMcpServerSampling 到 MCP scoped services
src/DotNetCampus.ModelContextProtocol/Exceptions/McpSamplingRejectedException.cs 新增 Sampling 被用户拒绝的专用异常
src/DotNetCampus.ModelContextProtocol/Exceptions/McpSamplingNotSupportedException.cs 新增客户端未声明 Sampling 能力时的专用异常
src/DotNetCampus.ModelContextProtocol/Exceptions/McpExceptionData.cs 切换到统一的内部 JSON 上下文进行序列化
src/DotNetCampus.ModelContextProtocol/CompilerServices/McpJsonContext.cs 合并/统一为 McpInternalJsonContext 覆盖协议内所有序列化类型(含 Sampling)
src/DotNetCampus.ModelContextProtocol/Clients/McpClientBuilder.cs 新增 WithSamplingHandler 配置并自动声明 capability;Build 时注入到 transport manager
src/DotNetCampus.ModelContextProtocol/Clients/McpClient.cs 客户端 API 调用序列化上下文切换到 McpInternalJsonContext
src/DotNetCampus.ModelContextProtocol.TouchSocket.Http/Transports/TouchSocket/TouchSocketHttpServerTransportSession.cs TouchSocket HTTP session 增加服务器发起请求等待/响应路由(pending 请求表)
src/DotNetCampus.ModelContextProtocol.TouchSocket.Http/Transports/TouchSocket/TouchSocketHttpServerTransport.cs TouchSocket POST body 支持区分 request/response;response 路由回 session;注入 transport session 服务
src/DotNetCampus.ModelContextProtocol.Ipc/Transports/Ipc/IpcServerTransportSession.cs IPC session 增加 pending 请求与 response 路由结构(但发送实现仍待完善)
samples/DotNetCampus.SampleMcpServer/Program.cs sample server 工具列表更新:加入 SamplingTool,并重命名/替换示例工具
samples/DotNetCampus.SampleMcpServer/McpTools/SamplingTool.cs 新增 sample 工具:人工验证 sampling/createMessage 流程与异常分支
samples/DotNetCampus.SampleMcpServer/McpTools/EchoDelayTool.cs 示例工具类重命名(SampleTool → EchoDelayTool)

@walterlv walterlv removed the request for review from lindexi April 7, 2026 03:42
@walterlv walterlv assigned walterlv and unassigned lindexi Apr 7, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 45 out of 45 changed files in this pull request and generated 5 comments.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 45 out of 45 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/DotNetCampus.ModelContextProtocol/Servers/IMcpServerPrimitiveContext.cs:12

  • 此文件不在 Protocol/ 下,但仍保留了大量中英双语 XML 注释;仓库规范要求仅 Protocol/ 下的消息类型使用双语注释,其他代码应为纯中文(见 .github/copilot-instructions.md:45)。既然本次已改动该接口,建议一并将这些遗留英文注释清理为纯中文,避免继续扩散。
/// <summary>
/// 包含 MCP 服务器收到来自客户端的请求时,服务端处理请求具体实现可能会用到的各种上下文信息。<br/>
/// Contains various context information that the server-side implementation of the MCP server
/// may use when the MCP server receives a request from the client.
/// </summary>

@walterlv
Copy link
Copy Markdown
Member Author

walterlv commented Apr 9, 2026

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Apr 9, 2026

@copilot apply changes based on the comments in this thread

已处理两个审查意见(commit a855395):

  1. PipeReader 资源释放 - 将 pipeReader.AsStream() 放入 using 块,并在 finally 中显式调用 pipeReader.CompleteAsync(),确保无论是否发生异常都能正确释放 PipeReader。

  2. 大小写不敏感属性查找 - 新增私有辅助方法 TryGetPropertyIgnoreCase,用 StringComparison.OrdinalIgnoreCase 遍历属性匹配,将 ClassifyAndDeserialize 中所有 element.TryGetProperty(...) 调用替换为该方法,与 McpInternalJsonContextPropertyNameCaseInsensitive = true 设置保持一致。

Copilot AI changed the title Sampling PR 审查意见:修复六项并发、协议、代码质量问题 修复 ServerTransportManager:PipeReader 未完成 + ClassifyAndDeserialize 大小写不一致 Apr 9, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 45 out of 45 changed files in this pull request and generated 10 comments.

@walterlv walterlv requested a review from lindexi April 9, 2026 05:05
@walterlv walterlv assigned lindexi and unassigned walterlv Apr 9, 2026
@walterlv walterlv changed the title 修复 ServerTransportManager:PipeReader 未完成 + ClassifyAndDeserialize 大小写不一致 增加 Sampling(委托生成)功能 Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants