-
Notifications
You must be signed in to change notification settings - Fork 631
Avoid string intermediates in MCP transport read path #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-string-encoding-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+522
−92
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eab4708
Initial plan
Copilot ed1ed14
Avoid string intermediates in MCP transport read side using PipeReader
Copilot b530d53
Factor out PipeReader line-reading into shared PipeReaderExtensions h…
Copilot 09cf5eb
Address review feedback: move GetUtf8String to EncodingUtilities, rem…
Copilot abb1879
Add PipeReaderExtensionsTests covering line-splitting edge cases
Copilot d050187
Add test for multi-byte sequence interrupted by newline
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/ModelContextProtocol.Core/Protocol/PipeReaderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| using System.Buffers; | ||
| using System.IO.Pipelines; | ||
|
|
||
| namespace ModelContextProtocol.Protocol; | ||
|
|
||
| /// <summary>Internal helper for reading newline-delimited UTF-8 lines from a <see cref="PipeReader"/>.</summary> | ||
| internal static class PipeReaderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Reads newline-delimited lines from <paramref name="reader"/>, invoking | ||
| /// <paramref name="processLine"/> for each non-empty line, until the reader signals completion. | ||
| /// </summary> | ||
| internal static async Task ReadLinesAsync( | ||
| this PipeReader reader, | ||
| Func<ReadOnlySequence<byte>, CancellationToken, Task> processLine, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| while (true) | ||
| { | ||
| ReadResult result = await reader.ReadAsync(cancellationToken).ConfigureAwait(false); | ||
| ReadOnlySequence<byte> buffer = result.Buffer; | ||
|
|
||
| SequencePosition? position; | ||
| while ((position = buffer.PositionOf((byte)'\n')) != null) | ||
| { | ||
| ReadOnlySequence<byte> line = buffer.Slice(0, position.Value); | ||
|
|
||
| // Trim trailing \r for Windows-style CRLF line endings. | ||
| if (EndsWithCarriageReturn(line)) | ||
| { | ||
| line = line.Slice(0, line.Length - 1); | ||
| } | ||
|
|
||
| if (!line.IsEmpty) | ||
| { | ||
| await processLine(line, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| // Advance past the '\n'. | ||
| buffer = buffer.Slice(buffer.GetPosition(1, position.Value)); | ||
| } | ||
|
|
||
| reader.AdvanceTo(buffer.Start, buffer.End); | ||
|
|
||
| if (result.IsCompleted) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static bool EndsWithCarriageReturn(in ReadOnlySequence<byte> sequence) | ||
| { | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (sequence.IsSingleSegment) | ||
| { | ||
| ReadOnlySpan<byte> span = sequence.First.Span; | ||
| return span.Length > 0 && span[span.Length - 1] == (byte)'\r'; | ||
| } | ||
|
|
||
| // Multi-segment: find the last non-empty segment to check its last byte. | ||
| ReadOnlyMemory<byte> last = default; | ||
| foreach (ReadOnlyMemory<byte> segment in sequence) | ||
| { | ||
| if (!segment.IsEmpty) | ||
| { | ||
| last = segment; | ||
| } | ||
| } | ||
|
|
||
| return !last.IsEmpty && last.Span[last.Length - 1] == (byte)'\r'; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.