The transport, the handshake and the Host API calls now come from the shared SDK, so this package only routes channel messages and the host's JSON-RPC error codes reach the MCP tools.
117 lines
3.6 KiB
Go
117 lines
3.6 KiB
Go
// Package sidecar serves this plugin's MCP channels over the Reims-Go Sidecar
|
|
// protocol. The transport, the handshake and the Host API calls come from
|
|
// pkg/pluginsdk, so this package only routes channel messages.
|
|
package sidecar
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/Nwflower/reims-go/pkg/pluginsdk"
|
|
)
|
|
|
|
// Channels lists the channels this sidecar serves. "mcp" carries the core
|
|
// computer-use tools and "mcp-dev" adds guest, snapshot and test tools.
|
|
var Channels = []string{"mcp", "mcp-dev"}
|
|
|
|
// MCPHandler answers MCP messages. session identifies one client connection;
|
|
// SessionClosed is called once that connection has ended.
|
|
type MCPHandler interface {
|
|
Handle(ctx context.Context, channel, session string, message json.RawMessage) (json.RawMessage, error)
|
|
SessionClosed(channel, session string)
|
|
}
|
|
|
|
// HostClient adapts the SDK server to the Call shape the MCP tools use. It
|
|
// keeps the JSON-RPC error code the host answered with, so a caller can tell a
|
|
// rejected request from a failed one.
|
|
type HostClient struct{ Server *Server }
|
|
|
|
func (c HostClient) Call(ctx context.Context, method string, params any, out any) error {
|
|
if c.Server == nil {
|
|
return errors.New("Reims-Go Host API is unavailable")
|
|
}
|
|
return c.Server.CallHost(ctx, method, params, out)
|
|
}
|
|
|
|
// Server routes the channels this plugin declares over one Sidecar connection.
|
|
type Server struct {
|
|
sdk *pluginsdk.Server
|
|
mcp MCPHandler
|
|
}
|
|
|
|
// New serves on the process's standard input and output.
|
|
func New(handler MCPHandler) *Server {
|
|
return NewWithIO(os.Stdin, os.Stdout, handler)
|
|
}
|
|
|
|
// NewWithIO serves over custom streams, which tests use.
|
|
func NewWithIO(r io.Reader, w io.Writer, handler MCPHandler) *Server {
|
|
s := &Server{sdk: pluginsdk.NewWithIO(r, w), mcp: handler}
|
|
s.sdk.SetCapability("channels", Channels)
|
|
for _, channel := range Channels {
|
|
s.sdk.Handle("channel/"+channel+"/message", s.channelMessage(channel))
|
|
s.sdk.Handle("channel/"+channel+"/closed", s.channelClosed(channel))
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Serve runs until the host closes the connection or sends plugin/shutdown.
|
|
func (s *Server) Serve(ctx context.Context) error {
|
|
return s.sdk.Serve(ctx)
|
|
}
|
|
|
|
// CallHost invokes one Host API method on Reims-Go.
|
|
func (s *Server) CallHost(ctx context.Context, method string, params, out any) error {
|
|
return s.sdk.CallHost(ctx, method, params, out)
|
|
}
|
|
|
|
// InitInfo returns the handshake metadata, including the features the host
|
|
// advertised.
|
|
func (s *Server) InitInfo() pluginsdk.InitInfo {
|
|
return s.sdk.InitInfo()
|
|
}
|
|
|
|
func (s *Server) channelMessage(channel string) pluginsdk.Handler {
|
|
return func(ctx context.Context, raw json.RawMessage) (any, error) {
|
|
var params struct {
|
|
Message json.RawMessage `json:"message"`
|
|
Session string `json:"session"`
|
|
}
|
|
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(params.Message) == 0 {
|
|
return nil, errors.New("channel message is missing")
|
|
}
|
|
if s.mcp == nil {
|
|
return nil, errors.New("no MCP handler is bound")
|
|
}
|
|
message, err := s.mcp.Handle(ctx, channel, params.Session, params.Message)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(message) == 0 {
|
|
message = json.RawMessage("null")
|
|
}
|
|
return map[string]json.RawMessage{"message": message}, nil
|
|
}
|
|
}
|
|
|
|
func (s *Server) channelClosed(channel string) pluginsdk.Handler {
|
|
return func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
var params struct {
|
|
Session string `json:"session"`
|
|
}
|
|
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
|
return nil, err
|
|
}
|
|
if params.Session != "" && s.mcp != nil {
|
|
s.mcp.SessionClosed(channel, params.Session)
|
|
}
|
|
return nil, nil
|
|
}
|
|
}
|