feat(mcp): add SSE transport support and related configuration options
Some checks failed
CI / build-and-test (push) Failing after -30m37s
Some checks failed
CI / build-and-test (push) Failing after -30m37s
This commit is contained in:
86
cmd/amcs-cli/cmd/sse.go
Normal file
86
cmd/amcs-cli/cmd/sse.go
Normal file
@@ -0,0 +1,86 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user