@@ -12,9 +12,53 @@ import (
12
12
"github.com/coder/coder/v2/codersdk/toolsdk"
13
13
)
14
14
15
+ type MCPToolset string
16
+
17
+ const (
18
+ MCPToolsetStandard MCPToolset = "standard"
19
+ MCPToolsetChatGPT MCPToolset = "chatgpt"
20
+ )
21
+
15
22
// mcpHTTPHandler creates the MCP HTTP transport handler
16
- func (api * API ) mcpHTTPHandler (tools []toolsdk.GenericTool ) http.Handler {
23
+ // It supports a "toolset" query parameter to select the set of tools to register.
24
+ func (api * API ) mcpHTTPHandler () http.Handler {
17
25
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
26
+ toolset := MCPToolset (r .URL .Query ().Get ("toolset" ))
27
+ // Default to standard toolset if no toolset is specified.
28
+ if toolset == "" {
29
+ toolset = MCPToolsetStandard
30
+ }
31
+
32
+ mcpTools := []toolsdk.GenericTool {}
33
+ switch toolset {
34
+ case MCPToolsetStandard :
35
+ // Register all available tools, but exclude:
36
+ // - ReportTask - which requires dependencies not available in the remote MCP context
37
+ // - ChatGPT search and fetch tools, which are redundant with the standard tools.
38
+ for _ , tool := range toolsdk .All {
39
+ if tool .Name == toolsdk .ToolNameReportTask ||
40
+ tool .Name == toolsdk .ToolNameChatGPTSearch || tool .Name == toolsdk .ToolNameChatGPTFetch {
41
+ continue
42
+ }
43
+ mcpTools = append (mcpTools , tool )
44
+ }
45
+ case MCPToolsetChatGPT :
46
+ // ChatGPT tools are the search and fetch tools as defined in https://platform.openai.com/docs/mcp.
47
+ // We do not expose any extra ones because ChatGPT has an undocumented "Safety Scan" feature.
48
+ // In my experiments, if I included extra tools in the MCP server, ChatGPT would often - but not always -
49
+ // refuse to add Coder as a connector.
50
+ for _ , tool := range toolsdk .All {
51
+ if tool .Name == toolsdk .ToolNameChatGPTSearch || tool .Name == toolsdk .ToolNameChatGPTFetch {
52
+ mcpTools = append (mcpTools , tool )
53
+ }
54
+ }
55
+ default :
56
+ httpapi .Write (r .Context (), w , http .StatusBadRequest , codersdk.Response {
57
+ Message : "Invalid toolset" ,
58
+ })
59
+ return
60
+ }
61
+
18
62
// Create MCP server instance for each request
19
63
mcpServer , err := mcp .NewServer (api .Logger .Named ("mcp" ))
20
64
if err != nil {
@@ -30,45 +74,11 @@ func (api *API) mcpHTTPHandler(tools []toolsdk.GenericTool) http.Handler {
30
74
authenticatedClient .SetSessionToken (httpmw .APITokenFromRequest (r ))
31
75
32
76
// Register tools with authenticated client
33
- if err := mcpServer .RegisterTools (authenticatedClient , tools ); err != nil {
77
+ if err := mcpServer .RegisterTools (authenticatedClient , mcpTools ); err != nil {
34
78
api .Logger .Warn (r .Context (), "failed to register MCP tools" , slog .Error (err ))
35
79
}
36
80
37
81
// Handle the MCP request
38
82
mcpServer .ServeHTTP (w , r )
39
83
})
40
84
}
41
-
42
- // standardMCPHTTPHandler sets up the MCP HTTP transport handler for the standard tools.
43
- // Standard tools are all tools except for the report task, ChatGPT search, and ChatGPT fetch tools.
44
- func (api * API ) standardMCPHTTPHandler () http.Handler {
45
- mcpTools := []toolsdk.GenericTool {}
46
- // Register all available tools, but exclude:
47
- // - ReportTask - which requires dependencies not available in the remote MCP context
48
- // - ChatGPT search and fetch tools, which are redundant with the standard tools.
49
- for _ , tool := range toolsdk .All {
50
- if tool .Name == toolsdk .ToolNameReportTask ||
51
- tool .Name == toolsdk .ToolNameChatGPTSearch || tool .Name == toolsdk .ToolNameChatGPTFetch {
52
- continue
53
- }
54
- mcpTools = append (mcpTools , tool )
55
- }
56
- return api .mcpHTTPHandler (mcpTools )
57
- }
58
-
59
- // chatgptMCPHTTPHandler sets up the MCP HTTP transport handler for the ChatGPT tools.
60
- // ChatGPT tools are the search and fetch tools as defined in https://platform.openai.com/docs/mcp.
61
- // We do not expose any extra ones because ChatGPT has an undocumented "Safety Scan" feature.
62
- // In my experiments, if I included extra tools in the MCP server, ChatGPT would refuse
63
- // to add Coder as a connector.
64
- func (api * API ) chatgptMCPHTTPHandler () http.Handler {
65
- mcpTools := []toolsdk.GenericTool {}
66
- // Register only the ChatGPT search and fetch tools.
67
- for _ , tool := range toolsdk .All {
68
- if ! (tool .Name == toolsdk .ToolNameChatGPTSearch || tool .Name == toolsdk .ToolNameChatGPTFetch ) {
69
- continue
70
- }
71
- mcpTools = append (mcpTools , tool )
72
- }
73
- return api .mcpHTTPHandler (mcpTools )
74
- }
0 commit comments