Pass context from request endpoint to message handler

Đang mở
#236 2 bình luận 8 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
32/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu từ McpEndpointRouteBuilderExtensions và McpServer, lần theo OnMessageReceivedAsync và lệnh gọi RequestHandlers.Set được hiển thị trong issue. Xác định cách một caller context opaque có thể đi cùng IJsonRpcMessage đến tool handler hoặc MCP request context. Hoàn thành khi context do endpoint cung cấp khả dụng trong các lệnh gọi tool chạy lâu mà không lưu trữ hoặc truyền HttpContext đã hoàn tất.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement needs confirmation

Is your feature request related to a problem? Please describe.
When the server is started, it runs in a scope that has the current HttpContext. In that context there might be useful headers that are relevant for that session. But when you need to call a tool the message endpoint might have a different HttpContext and different headers.
For example you might want to use the bearer token from the message endpoint to call a tool or to get the list of tools. In the case of SSE transport, you will need to transfer this token to the tool handler.

Describe the solution you'd like
Be able to build an object (could be opaque) on the message endpoint (can contain anything you want, you can include headers or other info) and pass that object alongside with the IJsonRpcMessage to the tool hander. Maybe make it available on the mcp request context.

Example call:

McpEndpointRouteBuilderExtensions:

await transport.OnMessageReceivedAsync(message, callerContext, context.RequestAborted);

McpServer:

RequestHandlers.Set(
    RequestMethods.ToolsCall,
    (request, callerContext, cancellationToken) => callToolHandler(new(this, request, callerContext), cancellationToken),
    McpJsonUtilities.JsonContext.Default.CallToolRequestParams,
    McpJsonUtilities.JsonContext.Default.CallToolResponse);

This could be extended to other requests as well.

Describe alternatives you've considered
Storing the context in memory introduces additional overhead since it has to be cleaned up and introduces unneeded complexity.
Storing the context in a cache is not ok because we might want to pass sensitive information.
Passing the http context directly is not ok because the post /message request could be finished by the time the tool call is actually processed.

Additional context
In this case we have tool calls with long timeout, they might take a long time to complete, in the order of minutes at least.

Ngôn ngữ chính
C#
Star
4.5k
Fork
814
Merge trung bình
9 ngày 19 giờ
Pull request đã merge (30 ngày)
4

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/csharp-sdk

Tất cả issue của modelcontextprotocol/csharp-sdk

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.