63 lines
2.3 KiB
Go
63 lines
2.3 KiB
Go
// Package tasktools adapts task-scoped central tools to the Gateway contract.
|
|
package tasktools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"google.golang.org/grpc"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
|
|
)
|
|
|
|
// Client never accepts an identity, org or permission list from the harness.
|
|
type Client struct{ rpc pb.TaskToolsServiceClient }
|
|
|
|
func NewClient(conn grpc.ClientConnInterface) *Client {
|
|
return &Client{rpc: pb.NewTaskToolsServiceClient(conn)}
|
|
}
|
|
|
|
func (c *Client) List(ctx context.Context, credential string) ([]*mcp.Tool, error) {
|
|
r, err := c.rpc.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: credential})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var tools []*mcp.Tool
|
|
err = json.Unmarshal(r.ToolsJson, &tools)
|
|
return tools, err
|
|
}
|
|
|
|
func (c *Client) Call(ctx context.Context, credential, name string, args json.RawMessage) (*mcp.CallToolResult, error) {
|
|
r, err := c.rpc.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: credential, Name: name, ArgumentsJson: args})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result mcp.CallToolResult
|
|
err = json.Unmarshal(r.ResultJson, &result)
|
|
return &result, err
|
|
}
|
|
|
|
func (c *Client) Close(ctx context.Context, credential string) error {
|
|
_, err := c.rpc.CloseTaskTools(ctx, &pb.CloseTaskToolsRequest{RunCredential: credential})
|
|
return err
|
|
}
|
|
|
|
// Dial opens the Gateway's task-tools contract as the authenticated daemon.
|
|
func Dial(target string, id *rpctls.Identity) (*Client, func(), error) {
|
|
creds, err := id.DialOption(target, presence.Gateway)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
conn, err := grpc.NewClient(target, creds, telemetry.ClientOption(), grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor(presence.Daemon, "")), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(messaging.MaxInboundAttachmentBytes+(1<<20))))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return NewClient(conn), func() { _ = conn.Close() }, nil
|
|
}
|