-
Notifications
You must be signed in to change notification settings - Fork 928
feat: implement MCP HTTP server endpoint with authentication #18670
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
Open
ThomasK33
wants to merge
1
commit into
thomask33/06-30-feat_oauth2_remove_unique_constraint_on_app_names_for_rfc_7591_compliance
Choose a base branch
from
thomask33/06-30-feat_mcp_implement_mcp_http_server_with_toolsdk_integration
base: thomask33/06-30-feat_oauth2_remove_unique_constraint_on_app_names_for_rfc_7591_compliance
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,743
−22
Open
Changes from all commits
Commits
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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package mcp | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
|
||
"github.com/coder/coder/v2/buildinfo" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/codersdk/toolsdk" | ||
) | ||
|
||
const ( | ||
// MCPServerName is the name used for the MCP server. | ||
MCPServerName = "Coder" | ||
// MCPServerInstructions is the instructions text for the MCP server. | ||
MCPServerInstructions = "Coder MCP Server providing workspace and template management tools" | ||
) | ||
|
||
// Server represents an MCP HTTP server instance | ||
type Server struct { | ||
Logger slog.Logger | ||
|
||
// mcpServer is the underlying MCP server | ||
mcpServer *server.MCPServer | ||
|
||
// streamableServer handles HTTP transport | ||
streamableServer *server.StreamableHTTPServer | ||
} | ||
|
||
// NewServer creates a new MCP HTTP server | ||
func NewServer(logger slog.Logger) (*Server, error) { | ||
// Create the core MCP server | ||
mcpSrv := server.NewMCPServer( | ||
MCPServerName, | ||
buildinfo.Version(), | ||
server.WithInstructions(MCPServerInstructions), | ||
) | ||
|
||
// Create logger adapter for mcp-go | ||
mcpLogger := &mcpLoggerAdapter{logger: logger} | ||
|
||
// Create streamable HTTP server with configuration | ||
streamableServer := server.NewStreamableHTTPServer(mcpSrv, | ||
server.WithHeartbeatInterval(30*time.Second), | ||
server.WithLogger(mcpLogger), | ||
) | ||
|
||
return &Server{ | ||
Logger: logger, | ||
mcpServer: mcpSrv, | ||
streamableServer: streamableServer, | ||
}, nil | ||
} | ||
|
||
// ServeHTTP implements http.Handler interface | ||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
s.streamableServer.ServeHTTP(w, r) | ||
} | ||
|
||
// RegisterTools registers all available MCP tools with the server | ||
func (s *Server) RegisterTools(client *codersdk.Client) error { | ||
if client == nil { | ||
return xerrors.New("client cannot be nil: MCP HTTP server requires authenticated client") | ||
} | ||
|
||
// Create tool dependencies | ||
toolDeps, err := toolsdk.NewDeps(client) | ||
if err != nil { | ||
return xerrors.Errorf("failed to initialize tool dependencies: %w", err) | ||
} | ||
|
||
// Register all available tools | ||
for _, tool := range toolsdk.All { | ||
s.mcpServer.AddTools(mcpFromSDK(tool, toolDeps)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// mcpFromSDK adapts a toolsdk.Tool to go-mcp's server.ServerTool | ||
func mcpFromSDK(sdkTool toolsdk.GenericTool, tb toolsdk.Deps) server.ServerTool { | ||
if sdkTool.Schema.Properties == nil { | ||
panic("developer error: schema properties cannot be nil") | ||
} | ||
|
||
return server.ServerTool{ | ||
Tool: mcp.Tool{ | ||
Name: sdkTool.Name, | ||
Description: sdkTool.Description, | ||
InputSchema: mcp.ToolInputSchema{ | ||
Type: "object", | ||
Properties: sdkTool.Schema.Properties, | ||
Required: sdkTool.Schema.Required, | ||
}, | ||
}, | ||
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
var buf bytes.Buffer | ||
if err := json.NewEncoder(&buf).Encode(request.Params.Arguments); err != nil { | ||
return nil, xerrors.Errorf("failed to encode request arguments: %w", err) | ||
} | ||
result, err := sdkTool.Handler(ctx, tb, buf.Bytes()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &mcp.CallToolResult{ | ||
Content: []mcp.Content{ | ||
mcp.NewTextContent(string(result)), | ||
}, | ||
}, nil | ||
}, | ||
} | ||
} | ||
|
||
// mcpLoggerAdapter adapts slog.Logger to the mcp-go util.Logger interface | ||
type mcpLoggerAdapter struct { | ||
logger slog.Logger | ||
} | ||
|
||
func (l *mcpLoggerAdapter) Infof(format string, v ...any) { | ||
l.logger.Info(context.Background(), fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (l *mcpLoggerAdapter) Errorf(format string, v ...any) { | ||
l.logger.Error(context.Background(), fmt.Sprintf(format, v...)) | ||
} |
Oops, something went wrong.
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.
Do we want to hide this behind a flag or experiment perhaps?
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.
+1
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.
Sure, I added an experiment up the stack in: #18712