-
Notifications
You must be signed in to change notification settings - Fork 965
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
Next
Next commit
feat: search and fetch mcp tools
- Loading branch information
commit a61194dfa938ceb3411d5418152273e45c4fd04d
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,11 @@ import ( | |
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/coderd/mcp" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/codersdk/toolsdk" | ||
) | ||
|
||
// mcpHTTPHandler creates the MCP HTTP transport handler | ||
func (api *API) mcpHTTPHandler() http.Handler { | ||
func (api *API) mcpHTTPHandler(tools []toolsdk.GenericTool) http.Handler { | ||
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. I think we could simplify this a bit by making it a boolean called This also makes the handler less complex. |
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Create MCP server instance for each request | ||
mcpServer, err := mcp.NewServer(api.Logger.Named("mcp")) | ||
|
@@ -29,11 +30,45 @@ func (api *API) mcpHTTPHandler() http.Handler { | |
authenticatedClient.SetSessionToken(httpmw.APITokenFromRequest(r)) | ||
|
||
// Register tools with authenticated client | ||
if err := mcpServer.RegisterTools(authenticatedClient); err != nil { | ||
if err := mcpServer.RegisterTools(authenticatedClient, tools); err != nil { | ||
api.Logger.Warn(r.Context(), "failed to register MCP tools", slog.Error(err)) | ||
} | ||
|
||
// Handle the MCP request | ||
mcpServer.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
// standardMCPHTTPHandler sets up the MCP HTTP transport handler for the standard tools. | ||
// Standard tools are all tools except for the report task, ChatGPT search, and ChatGPT fetch tools. | ||
func (api *API) standardMCPHTTPHandler() http.Handler { | ||
mcpTools := []toolsdk.GenericTool{} | ||
// Register all available tools, but exclude: | ||
// - ReportTask - which requires dependencies not available in the remote MCP context | ||
// - ChatGPT search and fetch tools, which are redundant with the standard tools. | ||
for _, tool := range toolsdk.All { | ||
if tool.Name == toolsdk.ToolNameReportTask || | ||
tool.Name == toolsdk.ToolNameChatGPTSearch || tool.Name == toolsdk.ToolNameChatGPTFetch { | ||
continue | ||
} | ||
mcpTools = append(mcpTools, tool) | ||
} | ||
return api.mcpHTTPHandler(mcpTools) | ||
} | ||
|
||
// chatgptMCPHTTPHandler sets up the MCP HTTP transport handler for the ChatGPT tools. | ||
// ChatGPT tools are the search and fetch tools as defined in https://platform.openai.com/docs/mcp. | ||
// We do not expose any extra ones because ChatGPT has an undocumented "Safety Scan" feature. | ||
// In my experiments, if I included extra tools in the MCP server, ChatGPT would refuse | ||
// to add Coder as a connector. | ||
func (api *API) chatgptMCPHTTPHandler() http.Handler { | ||
mcpTools := []toolsdk.GenericTool{} | ||
// Register only the ChatGPT search and fetch tools. | ||
for _, tool := range toolsdk.All { | ||
if !(tool.Name == toolsdk.ToolNameChatGPTSearch || tool.Name == toolsdk.ToolNameChatGPTFetch) { | ||
continue | ||
} | ||
mcpTools = append(mcpTools, tool) | ||
} | ||
return api.mcpHTTPHandler(mcpTools) | ||
} |
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.
Could we leave the function as it was and add a new function called
RegisterChatGPTTools
instead?This would benefit us by allowing us to add exceptions for ChatGPT more easily on the server structure, and keeping the existing MCP endpoint and function calls as they are/will be.
(Also, we can then write a unit test and assert that a ChatGPT MCP server only has those two tools that we expect)