-
Notifications
You must be signed in to change notification settings - Fork 2.1k
MCP access part 4: mcputils #54880
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
Merged
Merged
MCP access part 4: mcputils #54880
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package mcputils | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/gravitational/teleport/lib/utils" | ||
| ) | ||
|
|
||
| // IsOKCloseError checks if provided error is a common close error that | ||
| // indicates the connection is ended. | ||
| func IsOKCloseError(err error) bool { | ||
| return errors.Is(err, io.ErrClosedPipe) || | ||
| utils.IsOKNetworkError(err) | ||
| } |
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,80 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package mcputils | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "github.com/hashicorp/golang-lru/v2/simplelru" | ||
| "github.com/mark3labs/mcp-go/mcp" | ||
| ) | ||
|
|
||
| // IDTracker tracks message information like method based on ID. IDTracker | ||
| // internally uses an LRU cache to keep track the last X messages to avoid | ||
| // growing infinitely. IDTracker is safe for concurrent use. | ||
| type IDTracker struct { | ||
| mu sync.Mutex | ||
| lruCache *simplelru.LRU[mcp.RequestId, mcp.MCPMethod] | ||
| } | ||
|
|
||
| // NewIDTracker creates a new IDTracker with provided maximum size. | ||
| func NewIDTracker(size int) (*IDTracker, error) { | ||
| lruCache, err := simplelru.NewLRU[mcp.RequestId, mcp.MCPMethod](size, nil) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return &IDTracker{ | ||
| lruCache: lruCache, | ||
| }, nil | ||
| } | ||
|
|
||
| // PushRequest tracks a request. Returns true if the request has been added to | ||
| // cache. | ||
| func (t *IDTracker) PushRequest(msg *JSONRPCRequest) bool { | ||
| if msg == nil || msg.ID.IsNil() || msg.Method == "" { | ||
| return false | ||
| } | ||
| t.mu.Lock() | ||
| defer t.mu.Unlock() | ||
| t.lruCache.Add(msg.ID, msg.Method) | ||
| return true | ||
| } | ||
|
|
||
| // PopByID retrieves the tracked information and remove it from the tracker. | ||
| func (t *IDTracker) PopByID(id mcp.RequestId) (mcp.MCPMethod, bool) { | ||
| if id.IsNil() { | ||
| return "", false | ||
| } | ||
|
|
||
| t.mu.Lock() | ||
| defer t.mu.Unlock() | ||
|
|
||
| retrieved, ok := t.lruCache.Get(id) | ||
| if !ok { | ||
| return "", false | ||
| } | ||
| t.lruCache.Remove(id) | ||
| return retrieved, true | ||
| } | ||
|
|
||
| // Len returns the size of the tracker cache. | ||
| func (t *IDTracker) Len() int { | ||
| return t.lruCache.Len() | ||
| } | ||
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,109 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package mcputils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIDTracker(t *testing.T) { | ||
| tracker, err := NewIDTracker(5) | ||
| require.NoError(t, err) | ||
| require.Empty(t, tracker.Len()) | ||
|
|
||
| t.Run("request missing ID not tracked", func(t *testing.T) { | ||
| require.False(t, tracker.PushRequest(&JSONRPCRequest{ | ||
| Method: "bad", | ||
| })) | ||
| require.Empty(t, tracker.Len()) | ||
| }) | ||
|
|
||
| t.Run("request tracked", func(t *testing.T) { | ||
| require.True(t, tracker.PushRequest(&JSONRPCRequest{ | ||
| ID: mcp.NewRequestId(0), | ||
| Method: mcp.MethodToolsList, | ||
| })) | ||
| require.Equal(t, 1, tracker.Len()) | ||
| }) | ||
|
|
||
| t.Run("pop unknown id", func(t *testing.T) { | ||
| unknownIDs := []mcp.RequestId{ | ||
| mcp.NewRequestId(5), | ||
| mcp.NewRequestId("0"), | ||
| mcp.NewRequestId(nil), | ||
| } | ||
| for id := range slices.Values(unknownIDs) { | ||
| t.Run(fmt.Sprintf("%T", id), func(t *testing.T) { | ||
| _, ok := tracker.PopByID(id) | ||
| require.False(t, ok) | ||
| require.Equal(t, 1, tracker.Len()) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("pop tracked id", func(t *testing.T) { | ||
| method, ok := tracker.PopByID(mcp.NewRequestId(0)) | ||
| require.True(t, ok) | ||
| require.Equal(t, mcp.MethodToolsList, method) | ||
| require.Empty(t, tracker.Len()) | ||
| }) | ||
|
|
||
| t.Run("track last 5", func(t *testing.T) { | ||
| for i := range 20 { | ||
| tracker.PushRequest(&JSONRPCRequest{ | ||
| ID: mcp.NewRequestId(i + 1), | ||
| Method: mcp.MethodToolsCall, | ||
| }) | ||
| require.LessOrEqual(t, tracker.Len(), 10) | ||
| } | ||
| for i := range 5 { | ||
| method, ok := tracker.PopByID(mcp.NewRequestId(20 - i)) | ||
| require.True(t, ok) | ||
| require.Equal(t, mcp.MethodToolsCall, method) | ||
| } | ||
| require.Empty(t, tracker.Len()) | ||
| }) | ||
| } | ||
|
|
||
| func BenchmarkIDTracker(b *testing.B) { | ||
| idTracker, err := NewIDTracker(100) | ||
| require.NoError(b, err) | ||
|
|
||
| for i := 0; i < 100; i++ { | ||
| idTracker.PushRequest(&JSONRPCRequest{ | ||
| ID: mcp.NewRequestId(i), | ||
| Method: mcp.MethodToolsList, | ||
| }) | ||
| } | ||
|
|
||
| // cpu: Apple M3 Pro | ||
| // BenchmarkIDTracker-12 12267649 81.85 ns/op | ||
| for b.Loop() { | ||
| idTracker.PushRequest(&JSONRPCRequest{ | ||
| ID: mcp.NewRequestId(2000), | ||
| Method: mcp.MethodToolsList, | ||
| }) | ||
| idTracker.PopByID(mcp.NewRequestId(2000)) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
Addreturns a value indicating whether the key was evicted or not.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's if an old key is evicted because of the size, which has different meaning than current return. unless we return two different bool. i will leave it out for now.