- Notifications
You must be signed in to change notification settings - Fork968
[WIP] Support completions for GH resources#450
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
Copilot wants to merge4 commits intomainChoose a base branch fromcopilot/fix-422
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.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
ba4e01d
Initial plan for issue
Copilot39ecfad
Implement basic completion support for GitHub resources
Copilot6dec5da
Add completion infrastructure and stdio server wrapper
Copilotaef9456
Implement working completion support with custom in-process client
CopilotFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package github | ||
import ( | ||
"context" | ||
"testing" | ||
"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func TestGitHubMCPServerCompletionIntegration(t *testing.T) { | ||
// Mock client function | ||
getClient := func(_ context.Context) (*github.Client, error) { | ||
// Return a nil client - this will cause API calls to fail gracefully | ||
// which is fine for testing the completion request handling flow | ||
return nil, nil | ||
} | ||
// Create a GitHub MCP server with completion support | ||
ghServer := NewGitHubServer("test", getClient) | ||
require.NotNil(t, ghServer) | ||
// Create an in-process client with our custom GitHubMCPServer transport | ||
mcpClient, err := NewInProcessClientWithGitHubServer(ghServer) | ||
require.NoError(t, err) | ||
// Initialize the client | ||
ctx := context.Background() | ||
request := mcp.InitializeRequest{} | ||
request.Params.ProtocolVersion = "2025-03-26" | ||
request.Params.ClientInfo = mcp.Implementation{ | ||
Name: "test-client", | ||
Version: "1.0.0", | ||
} | ||
result, err := mcpClient.Initialize(ctx, request) | ||
require.NoError(t, err) | ||
assert.Equal(t, "github-mcp-server", result.ServerInfo.Name) | ||
// Test completion request - this should work even with a nil GitHub client | ||
// because non-repo URIs return empty completions without calling GitHub APIs | ||
completionRequest := mcp.CompleteRequest{ | ||
Params: struct { | ||
Ref any `json:"ref"` | ||
Argument struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} `json:"argument"` | ||
}{ | ||
Ref: map[string]interface{}{ | ||
"type": "ref/resource", | ||
"uri": "file:///some/non-repo/path", | ||
}, | ||
Argument: struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
}{ | ||
Name: "param", | ||
Value: "test", | ||
}, | ||
}, | ||
} | ||
completionResult, err := mcpClient.Complete(ctx, completionRequest) | ||
require.NoError(t, err) | ||
require.NotNil(t, completionResult) | ||
// Should return empty completion for non-repo URIs | ||
assert.Equal(t, []string{}, completionResult.Completion.Values) | ||
assert.Equal(t, 0, completionResult.Completion.Total) | ||
// Test repo URI completion with unsupported argument | ||
repoCompletionRequest := mcp.CompleteRequest{ | ||
Params: struct { | ||
Ref any `json:"ref"` | ||
Argument struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} `json:"argument"` | ||
}{ | ||
Ref: map[string]interface{}{ | ||
"type": "ref/resource", | ||
"uri": "repo://{owner}/{repo}/contents{/path*}", | ||
}, | ||
Argument: struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
}{ | ||
Name: "unsupported", | ||
Value: "test", | ||
}, | ||
}, | ||
} | ||
repoCompletionResult, err := mcpClient.Complete(ctx, repoCompletionRequest) | ||
require.NoError(t, err) | ||
require.NotNil(t, repoCompletionResult) | ||
// Should return empty completion for unsupported arguments | ||
assert.Equal(t, []string{}, repoCompletionResult.Completion.Values) | ||
assert.Equal(t, 0, repoCompletionResult.Completion.Total) | ||
// Clean up | ||
err = mcpClient.Close() | ||
assert.NoError(t, err) | ||
} | ||
func TestGitHubMCPServerCompletionCapabilities(t *testing.T) { | ||
// Mock client function | ||
getClient := func(_ context.Context) (*github.Client, error) { | ||
return nil, nil | ||
} | ||
// Create a GitHub MCP server with completion support | ||
ghServer := NewGitHubServer("test", getClient) | ||
require.NotNil(t, ghServer) | ||
// Create an in-process client with our custom GitHubMCPServer transport | ||
mcpClient, err := NewInProcessClientWithGitHubServer(ghServer) | ||
require.NoError(t, err) | ||
// Initialize the client | ||
ctx := context.Background() | ||
request := mcp.InitializeRequest{} | ||
request.Params.ProtocolVersion = "2025-03-26" | ||
request.Params.ClientInfo = mcp.Implementation{ | ||
Name: "test-client", | ||
Version: "1.0.0", | ||
} | ||
result, err := mcpClient.Initialize(ctx, request) | ||
require.NoError(t, err) | ||
// Check basic server info | ||
assert.Equal(t, "github-mcp-server", result.ServerInfo.Name) | ||
// Clean up | ||
err = mcpClient.Close() | ||
assert.NoError(t, 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,141 @@ | ||
package github | ||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
// CompletionAwareStdioServer wraps the MCP stdio server to add completion support | ||
type CompletionAwareStdioServer struct { | ||
baseServer *server.MCPServer | ||
completionHandler CompletionHandlerFunc | ||
errLogger *log.Logger | ||
} | ||
// NewCompletionAwareStdioServer creates a new stdio server with completion support | ||
func NewCompletionAwareStdioServer(mcpServer *server.MCPServer, completionHandler CompletionHandlerFunc) *CompletionAwareStdioServer { | ||
return &CompletionAwareStdioServer{ | ||
baseServer: mcpServer, | ||
completionHandler: completionHandler, | ||
errLogger: log.New(io.Discard, "", 0), // Default to discarding errors | ||
} | ||
} | ||
// SetErrorLogger sets the error logger for the server | ||
func (s *CompletionAwareStdioServer) SetErrorLogger(logger *log.Logger) { | ||
s.errLogger = logger | ||
} | ||
// Listen starts the completion-aware stdio server | ||
func (s *CompletionAwareStdioServer) Listen(ctx context.Context, stdin io.Reader, stdout io.Writer) error { | ||
// Use the simplified approach: create a custom stdio server that mimics the real one | ||
// but intercepts completion requests | ||
// We'll use the real stdio server from the mcp-go library and intercept the raw messages | ||
realStdioServer := server.NewStdioServer(s.baseServer) | ||
realStdioServer.SetErrorLogger(s.errLogger) | ||
// Create pipes to intercept messages | ||
stdinPipe := &completionInterceptReader{ | ||
original: stdin, | ||
completionHandler: s.completionHandler, | ||
baseServer: s.baseServer, | ||
stdout: stdout, | ||
ctx: ctx, | ||
errLogger: s.errLogger, | ||
} | ||
return realStdioServer.Listen(ctx, stdinPipe, stdout) | ||
} | ||
// completionInterceptReader intercepts stdin to handle completion requests | ||
type completionInterceptReader struct { | ||
original io.Reader | ||
completionHandler CompletionHandlerFunc | ||
baseServer *server.MCPServer | ||
stdout io.Writer | ||
ctx context.Context | ||
errLogger *log.Logger | ||
buffer []byte | ||
bufferPos int | ||
} | ||
func (r *completionInterceptReader) Read(p []byte) (n int, err error) { | ||
// If we have buffered data, return that first | ||
if r.bufferPos < len(r.buffer) { | ||
n = copy(p, r.buffer[r.bufferPos:]) | ||
r.bufferPos += n | ||
if r.bufferPos >= len(r.buffer) { | ||
r.buffer = nil | ||
r.bufferPos = 0 | ||
} | ||
return n, nil | ||
} | ||
// Read from original source | ||
n, err = r.original.Read(p) | ||
if err != nil { | ||
return n, err | ||
} | ||
// Check if this contains a completion request | ||
data := p[:n] | ||
if r.isCompletionRequest(data) { | ||
// Handle completion request directly | ||
response := r.handleCompletionRequest(data) | ||
if response != nil { | ||
// Write response to stdout | ||
encoder := json.NewEncoder(r.stdout) | ||
if encErr := encoder.Encode(response); encErr != nil { | ||
r.errLogger.Printf("Error writing completion response: %v", encErr) | ||
} | ||
} | ||
// Return EOF to the real server so it doesn't process this message | ||
return 0, io.EOF | ||
} | ||
return n, err | ||
} | ||
// isCompletionRequest checks if the data contains a completion request | ||
func (r *completionInterceptReader) isCompletionRequest(data []byte) bool { | ||
var baseMessage struct { | ||
Method string `json:"method"` | ||
} | ||
if err := json.Unmarshal(data, &baseMessage); err != nil { | ||
return false | ||
} | ||
return baseMessage.Method == "completion/complete" | ||
} | ||
// handleCompletionRequest processes completion requests | ||
func (r *completionInterceptReader) handleCompletionRequest(data []byte) mcp.JSONRPCMessage { | ||
var baseMessage struct { | ||
JSONRPC string `json:"jsonrpc"` | ||
ID any `json:"id"` | ||
Method string `json:"method"` | ||
} | ||
if err := json.Unmarshal(data, &baseMessage); err != nil { | ||
return createErrorResponse(baseMessage.ID, mcp.PARSE_ERROR, "Failed to parse completion request") | ||
} | ||
var request mcp.CompleteRequest | ||
if err := json.Unmarshal(data, &request); err != nil { | ||
return createErrorResponse(baseMessage.ID, mcp.INVALID_REQUEST, "Failed to parse completion request") | ||
} | ||
result, err := r.completionHandler(r.ctx, request) | ||
if err != nil { | ||
return createErrorResponse(baseMessage.ID, mcp.INTERNAL_ERROR, err.Error()) | ||
} | ||
return createResponse(baseMessage.ID, *result) | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.