-
Notifications
You must be signed in to change notification settings - Fork 968
feat: add MCP tools for ChatGPT #19102
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
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a61194d
feat: search and fetch mcp tools
hugodutka a48445e
tests
hugodutka 50bae9d
feat: pass search query directly to endpoints
hugodutka ec57659
chore: update tests
hugodutka f86a119
chore: move getServerURL under Deps
hugodutka 2faad08
chore: replace dedicated chatgpt endpoint with query param
hugodutka 4afd149
chore: nits
hugodutka 9f938b9
chore: refactor RegisterTools
hugodutka e50627d
fix: typo
hugodutka 9fef13e
chore: update test
hugodutka 5efa6d9
chore: rename getServerURL to ServerURL
hugodutka 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
tests
- Loading branch information
commit a48445e269c566f74d9deee0583de81e6965960b
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1215,6 +1215,155 @@ func TestMCPHTTP_E2E_OAuth2_EndToEnd(t *testing.T) { | |||||
}) | ||||||
} | ||||||
|
||||||
func TestMCPHTTP_E2E_ChatGPTEndpoint(t *testing.T) { | ||||||
t.Parallel() | ||||||
|
||||||
// Setup Coder server with authentication | ||||||
coderClient, closer, api := coderdtest.NewWithAPI(t, &coderdtest.Options{ | ||||||
IncludeProvisionerDaemon: true, | ||||||
}) | ||||||
defer closer.Close() | ||||||
|
||||||
user := coderdtest.CreateFirstUser(t, coderClient) | ||||||
|
||||||
// Create template and workspace for testing search functionality | ||||||
version := coderdtest.CreateTemplateVersion(t, coderClient, user.OrganizationID, nil) | ||||||
coderdtest.AwaitTemplateVersionJobCompleted(t, coderClient, version.ID) | ||||||
template := coderdtest.CreateTemplate(t, coderClient, user.OrganizationID, version.ID) | ||||||
|
||||||
// Create MCP client pointing to the ChatGPT endpoint | ||||||
mcpURL := api.AccessURL.String() + "/api/experimental/mcp/chatgpt" | ||||||
|
||||||
// Configure client with authentication headers using RFC 6750 Bearer token | ||||||
mcpClient, err := mcpclient.NewStreamableHttpClient(mcpURL, | ||||||
transport.WithHTTPHeaders(map[string]string{ | ||||||
"Authorization": "Bearer " + coderClient.SessionToken(), | ||||||
})) | ||||||
require.NoError(t, err) | ||||||
defer func() { | ||||||
if closeErr := mcpClient.Close(); closeErr != nil { | ||||||
t.Logf("Failed to close MCP client: %v", closeErr) | ||||||
} | ||||||
}() | ||||||
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
defer cancel() | ||||||
|
||||||
// Start client | ||||||
err = mcpClient.Start(ctx) | ||||||
require.NoError(t, err) | ||||||
|
||||||
// Initialize connection | ||||||
initReq := mcp.InitializeRequest{ | ||||||
Params: mcp.InitializeParams{ | ||||||
ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, | ||||||
ClientInfo: mcp.Implementation{ | ||||||
Name: "test-chatgpt-client", | ||||||
Version: "1.0.0", | ||||||
}, | ||||||
}, | ||||||
} | ||||||
|
||||||
result, err := mcpClient.Initialize(ctx, initReq) | ||||||
require.NoError(t, err) | ||||||
require.Equal(t, mcpserver.MCPServerName, result.ServerInfo.Name) | ||||||
require.Equal(t, mcp.LATEST_PROTOCOL_VERSION, result.ProtocolVersion) | ||||||
require.NotNil(t, result.Capabilities) | ||||||
|
||||||
// Test tool listing - should only have search and fetch tools for ChatGPT | ||||||
tools, err := mcpClient.ListTools(ctx, mcp.ListToolsRequest{}) | ||||||
require.NoError(t, err) | ||||||
require.NotEmpty(t, tools.Tools) | ||||||
|
||||||
// Verify we have exactly the ChatGPT tools and no others | ||||||
var foundTools []string | ||||||
for _, tool := range tools.Tools { | ||||||
foundTools = append(foundTools, tool.Name) | ||||||
} | ||||||
|
||||||
// ChatGPT endpoint should only expose search and fetch tools | ||||||
assert.Contains(t, foundTools, toolsdk.ToolNameChatGPTSearch, "Should have ChatGPT search tool") | ||||||
assert.Contains(t, foundTools, toolsdk.ToolNameChatGPTFetch, "Should have ChatGPT fetch tool") | ||||||
assert.Len(t, foundTools, 2, "ChatGPT endpoint should only expose search and fetch tools") | ||||||
|
||||||
// Should NOT have other tools that are available in the standard endpoint | ||||||
assert.NotContains(t, foundTools, toolsdk.ToolNameGetAuthenticatedUser, "Should not have authenticated user tool") | ||||||
assert.NotContains(t, foundTools, toolsdk.ToolNameListWorkspaces, "Should not have list workspaces tool") | ||||||
|
||||||
t.Logf("ChatGPT endpoint tools: %v", foundTools) | ||||||
|
||||||
// Test search tool - search for templates | ||||||
var searchTool *mcp.Tool | ||||||
for _, tool := range tools.Tools { | ||||||
if tool.Name == toolsdk.ToolNameChatGPTSearch { | ||||||
searchTool = &tool | ||||||
break | ||||||
} | ||||||
} | ||||||
require.NotNil(t, searchTool, "Expected to find search tool") | ||||||
|
||||||
// Execute search for templates | ||||||
searchReq := mcp.CallToolRequest{ | ||||||
Params: mcp.CallToolParams{ | ||||||
Name: searchTool.Name, | ||||||
Arguments: map[string]any{ | ||||||
"query": "templates", | ||||||
}, | ||||||
}, | ||||||
} | ||||||
|
||||||
searchResult, err := mcpClient.CallTool(ctx, searchReq) | ||||||
require.NoError(t, err) | ||||||
require.NotEmpty(t, searchResult.Content) | ||||||
|
||||||
// Verify the search result contains our template | ||||||
assert.Len(t, searchResult.Content, 1) | ||||||
if textContent, ok := searchResult.Content[0].(mcp.TextContent); ok { | ||||||
assert.Equal(t, "text", textContent.Type) | ||||||
assert.Contains(t, textContent.Text, template.ID.String(), "Search result should contain our test template") | ||||||
t.Logf("Search result: %s", textContent.Text) | ||||||
} else { | ||||||
t.Errorf("Expected TextContent type, got %T", searchResult.Content[0]) | ||||||
} | ||||||
|
||||||
// Test fetch tool | ||||||
var fetchTool *mcp.Tool | ||||||
for _, tool := range tools.Tools { | ||||||
if tool.Name == toolsdk.ToolNameChatGPTFetch { | ||||||
fetchTool = &tool | ||||||
break | ||||||
} | ||||||
} | ||||||
require.NotNil(t, fetchTool, "Expected to find fetch tool") | ||||||
|
||||||
// Execute fetch for the template | ||||||
fetchReq := mcp.CallToolRequest{ | ||||||
Params: mcp.CallToolParams{ | ||||||
Name: fetchTool.Name, | ||||||
Arguments: map[string]any{ | ||||||
"id": fmt.Sprintf("template:%s", template.ID.String()), | ||||||
}, | ||||||
}, | ||||||
} | ||||||
|
||||||
fetchResult, err := mcpClient.CallTool(ctx, fetchReq) | ||||||
require.NoError(t, err) | ||||||
require.NotEmpty(t, fetchResult.Content) | ||||||
|
||||||
// Verify the fetch result contains template details | ||||||
assert.Len(t, fetchResult.Content, 1) | ||||||
if textContent, ok := fetchResult.Content[0].(mcp.TextContent); ok { | ||||||
assert.Equal(t, "text", textContent.Type) | ||||||
assert.Contains(t, textContent.Text, template.Name, "Fetch result should contain template name") | ||||||
assert.Contains(t, textContent.Text, template.ID.String(), "Fetch result should contain template ID") | ||||||
t.Logf("Fetch result contains template data") | ||||||
} else { | ||||||
t.Errorf("Expected TextContent type, got %T", fetchResult.Content[0]) | ||||||
} | ||||||
|
||||||
t.Logf("ChatGPT endpoint E2E test successful: search and fetch tools working correctly") | ||||||
} | ||||||
|
||||||
// Helper function to parse URL safely in tests | ||||||
func mustParseURL(t *testing.T, rawURL string) *url.URL { | ||||||
u, err := url.Parse(rawURL) | ||||||
|
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.