Exposes core CLI commands as MCP tools for AI agents. This change introduces a Go-based MCP server that wraps the existing core CLI commands (`go test`, `dev health`, `dev commit`), providing structured JSON responses. This allows AI agents to interact with the core CLI in a structured, type-safe manner. The implementation includes: - A new Go HTTP server in `google/mcp/` - Handlers for each of the core CLI commands - Unit tests for the handlers with a mock `core` executable - Documentation for the new MCP tools - Integration with the `code` plugin via `plugin.json`
112 lines
2.5 KiB
Go
112 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Get the absolute path to the testdata directory
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
testdataPath := filepath.Join(wd, "testdata")
|
|
|
|
// Add the absolute path to the PATH
|
|
os.Setenv("PATH", testdataPath+":"+os.Getenv("PATH"))
|
|
m.Run()
|
|
}
|
|
func TestGoTestHandler(t *testing.T) {
|
|
reqBody := GoTestRequest{
|
|
Filter: "TestMyFunction",
|
|
Coverage: true,
|
|
}
|
|
body, _ := json.Marshal(reqBody)
|
|
|
|
req, err := http.NewRequest("POST", "/core_go_test", bytes.NewBuffer(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler := http.HandlerFunc(goTestHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
var resp GoTestResponse
|
|
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("could not decode response: %v", err)
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
t.Errorf("handler returned an unexpected error: %v", resp.Error)
|
|
}
|
|
}
|
|
|
|
func TestDevHealthHandler(t *testing.T) {
|
|
req, err := http.NewRequest("POST", "/core_dev_health", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler := http.HandlerFunc(devHealthHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
var resp DevHealthResponse
|
|
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("could not decode response: %v", err)
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
t.Errorf("handler returned an unexpected error: %v", resp.Error)
|
|
}
|
|
}
|
|
|
|
func TestDevCommitHandler(t *testing.T) {
|
|
reqBody := DevCommitRequest{
|
|
Message: "test commit",
|
|
}
|
|
body, _ := json.Marshal(reqBody)
|
|
|
|
req, err := http.NewRequest("POST", "/core_dev_commit", bytes.NewBuffer(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler := http.HandlerFunc(devCommitHandler)
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
var resp DevCommitResponse
|
|
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("could not decode response: %v", err)
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
t.Errorf("handler returned an unexpected error: %v", resp.Error)
|
|
}
|
|
}
|