|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package orchestrator |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "os/exec" |
| 23 | + |
| 24 | + apiv1 "github.com/google/android-cuttlefish/frontend/src/host_orchestrator/api/v1" |
| 25 | + "github.com/google/uuid" |
| 26 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 27 | +) |
| 28 | + |
| 29 | +type ListCVDsParams struct { |
| 30 | + Group string `json:"group,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +func NewMCPHandler(config Config, om OperationManager, uam UserArtifactsManager) http.Handler { |
| 34 | + server := mcp.NewServer(&mcp.Implementation{Name: "host-orchestrator", Version: "1.0"}, &mcp.ServerOptions{ |
| 35 | + InitializedHandler: func(context.Context, *mcp.InitializedRequest) { |
| 36 | + fmt.Println("initialized!") |
| 37 | + }, |
| 38 | + GetSessionID: func() string { |
| 39 | + return "" |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + mcp.AddTool(server, &mcp.Tool{ |
| 44 | + Name: "list_cvds", |
| 45 | + Description: "List all cuttlefish instances", |
| 46 | + }, func(ctx context.Context, req *mcp.CallToolRequest, args ListCVDsParams) (*mcp.CallToolResult, any, error) { |
| 47 | + action := NewListCVDsAction(ListCVDsActionOpts{ |
| 48 | + Group: args.Group, |
| 49 | + Paths: config.Paths, |
| 50 | + ExecContext: exec.CommandContext, |
| 51 | + }) |
| 52 | + resp, err := action.Run() |
| 53 | + if err != nil { |
| 54 | + return nil, nil, err |
| 55 | + } |
| 56 | + jsonData, err := json.Marshal(resp) |
| 57 | + if err != nil { |
| 58 | + return nil, nil, err |
| 59 | + } |
| 60 | + return &mcp.CallToolResult{ |
| 61 | + Content: []mcp.Content{ |
| 62 | + &mcp.TextContent{Text: string(jsonData)}, |
| 63 | + }, |
| 64 | + }, nil, nil |
| 65 | + }) |
| 66 | + |
| 67 | + mcp.AddTool(server, &mcp.Tool{ |
| 68 | + Name: "create_cvd", |
| 69 | + Description: "Create cuttlefish instances", |
| 70 | + }, func(ctx context.Context, mcpReq *mcp.CallToolRequest, args apiv1.CreateCVDRequest) (*mcp.CallToolResult, any, error) { |
| 71 | + dummyReq, _ := http.NewRequest("POST", "http://dummy", nil) |
| 72 | + creds := getFetchCredentials(config.BuildAPICredentials, dummyReq) |
| 73 | + cvdBundleFetcher := newFetchCVDCommandArtifactsFetcher(exec.CommandContext, creds, config.AndroidBuildServiceURL) |
| 74 | + opts := CreateCVDActionOpts{ |
| 75 | + Request: &args, |
| 76 | + HostValidator: &HostValidator{ExecContext: exec.CommandContext}, |
| 77 | + Paths: config.Paths, |
| 78 | + OperationManager: om, |
| 79 | + ExecContext: exec.CommandContext, |
| 80 | + CVDBundleFetcher: cvdBundleFetcher, |
| 81 | + UUIDGen: func() string { return uuid.New().String() }, |
| 82 | + UserArtifactsDirResolver: uam, |
| 83 | + FetchCredentials: creds, |
| 84 | + BuildAPIBaseURL: config.AndroidBuildServiceURL, |
| 85 | + } |
| 86 | + resp, err := NewCreateCVDAction(opts).Run() |
| 87 | + if err != nil { |
| 88 | + return nil, nil, err |
| 89 | + } |
| 90 | + jsonData, err := json.Marshal(resp) |
| 91 | + if err != nil { |
| 92 | + return nil, nil, err |
| 93 | + } |
| 94 | + return &mcp.CallToolResult{ |
| 95 | + Content: []mcp.Content{ |
| 96 | + &mcp.TextContent{Text: string(jsonData)}, |
| 97 | + }, |
| 98 | + }, nil, nil |
| 99 | + }) |
| 100 | + |
| 101 | + return mcp.NewStreamableHTTPHandler(func(request *http.Request) *mcp.Server { |
| 102 | + return server |
| 103 | + }, &mcp.StreamableHTTPOptions{JSONResponse: true}) |
| 104 | +} |
0 commit comments