87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var sseCmd = &cobra.Command{
|
|
Use: "sse",
|
|
Short: "Run a stdio MCP bridge backed by a remote AMCS server using SSE transport (widely supported by hosted MCP clients)",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ctx := cmd.Context()
|
|
|
|
if err := requireServer(); err != nil {
|
|
return err
|
|
}
|
|
|
|
client := mcp.NewClient(&mcp.Implementation{Name: "amcs-cli", Version: "0.0.1"}, nil)
|
|
transport := &mcp.SSEClientTransport{
|
|
Endpoint: sseEndpointURL(),
|
|
HTTPClient: newHTTPClient(),
|
|
}
|
|
|
|
connectCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
remote, err := client.Connect(connectCtx, transport, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("connect to AMCS SSE endpoint: %w", err)
|
|
}
|
|
defer func() { _ = remote.Close() }()
|
|
|
|
tools, err := remote.ListTools(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("load remote tools: %w", err)
|
|
}
|
|
|
|
server := mcp.NewServer(&mcp.Implementation{
|
|
Name: "amcs-cli",
|
|
Title: "AMCS CLI Bridge (SSE)",
|
|
Version: "0.0.1",
|
|
}, nil)
|
|
|
|
for _, tool := range tools.Tools {
|
|
remoteTool := tool
|
|
server.AddTool(&mcp.Tool{
|
|
Name: remoteTool.Name,
|
|
Description: remoteTool.Description,
|
|
InputSchema: remoteTool.InputSchema,
|
|
OutputSchema: remoteTool.OutputSchema,
|
|
Annotations: remoteTool.Annotations,
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
return remote.CallTool(ctx, &mcp.CallToolParams{
|
|
Name: req.Params.Name,
|
|
Arguments: req.Params.Arguments,
|
|
})
|
|
})
|
|
}
|
|
|
|
session, err := server.Connect(ctx, &mcp.StdioTransport{}, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("start stdio bridge: %w", err)
|
|
}
|
|
defer func() { _ = session.Close() }()
|
|
|
|
<-ctx.Done()
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func sseEndpointURL() string {
|
|
base := strings.TrimRight(strings.TrimSpace(cfg.Server), "/")
|
|
if strings.HasSuffix(base, "/sse") {
|
|
return base
|
|
}
|
|
return base + "/sse"
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(sseCmd)
|
|
}
|