mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
bddb808b25
Fixes all our Go file imports to match the preferred spec that we've _mostly_ been using. For example: ``` import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "golang.org/x/xerrors" "gopkg.in/natefinch/lumberjack.v2" "cdr.dev/slog/v3" "github.com/coder/coder/v2/codersdk/agentsdk" "github.com/coder/serpent" ) ``` 3 groups: standard library, 3rd partly libs, Coder libs. This PR makes the change across the codebase. The PR in the stack above modifies our formatting to maintain this state of affairs, and is a separate PR so it's possible to review that one in detail.
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package coderd
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/coderd/httpmw"
|
|
"github.com/coder/coder/v2/coderd/mcp"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type MCPToolset string
|
|
|
|
const (
|
|
MCPToolsetStandard MCPToolset = "standard"
|
|
MCPToolsetChatGPT MCPToolset = "chatgpt"
|
|
)
|
|
|
|
// mcpHTTPHandler creates the MCP HTTP transport handler
|
|
// It supports a "toolset" query parameter to select the set of tools to register.
|
|
func (api *API) mcpHTTPHandler() 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"))
|
|
if err != nil {
|
|
api.Logger.Error(r.Context(), "failed to create MCP server", slog.Error(err))
|
|
httpapi.Write(r.Context(), w, http.StatusInternalServerError, codersdk.Response{
|
|
Message: "MCP server initialization failed",
|
|
})
|
|
return
|
|
}
|
|
// Extract the original session token from the request
|
|
authenticatedClient := codersdk.New(api.AccessURL,
|
|
codersdk.WithSessionToken(httpmw.APITokenFromRequest(r)))
|
|
toolset := MCPToolset(r.URL.Query().Get("toolset"))
|
|
// Default to standard toolset if no toolset is specified.
|
|
if toolset == "" {
|
|
toolset = MCPToolsetStandard
|
|
}
|
|
|
|
switch toolset {
|
|
case MCPToolsetStandard:
|
|
if err := mcpServer.RegisterTools(authenticatedClient); err != nil {
|
|
api.Logger.Warn(r.Context(), "failed to register MCP tools", slog.Error(err))
|
|
}
|
|
case MCPToolsetChatGPT:
|
|
if err := mcpServer.RegisterChatGPTTools(authenticatedClient); err != nil {
|
|
api.Logger.Warn(r.Context(), "failed to register MCP tools", slog.Error(err))
|
|
}
|
|
default:
|
|
httpapi.Write(r.Context(), w, http.StatusBadRequest, codersdk.Response{
|
|
Message: fmt.Sprintf("Invalid toolset: %s", toolset),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Handle the MCP request
|
|
mcpServer.ServeHTTP(w, r)
|
|
})
|
|
}
|