- Notifications
You must be signed in to change notification settings - Fork1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
a61194d
a48445e
50bae9d
ec57659
f86a119
2faad08
4afd149
9f938b9
e50627d
9fef13e
5efa6d9
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -67,8 +67,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
s.streamableServer.ServeHTTP(w, r) | ||
} | ||
// RegisterTools registers MCP tools with the server | ||
func (s *Server) RegisterTools(client *codersdk.Client, tools []toolsdk.GenericTool) error { | ||
| ||
if client == nil { | ||
return xerrors.New("client cannot be nil: MCP HTTP server requires authenticated client") | ||
} | ||
@@ -79,13 +79,7 @@ func (s *Server) RegisterTools(client *codersdk.Client) error { | ||
return xerrors.Errorf("failed to initialize tool dependencies: %w", err) | ||
} | ||
for _, tool := range tools { | ||
s.mcpServer.AddTools(mcpFromSDK(tool, toolDeps)) | ||
} | ||
return nil | ||
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(tools []toolsdk.GenericTool) http.Handler { | ||
| ||
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, 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) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.