| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "os/signal" |
| 13 | "runtime" |
| 14 | "time" |
| 15 | |
| 16 | access "amadan.net/oficina/agent-access.git" |
| 17 | "amadan.net/oficina/agent-access.git/client" |
| 18 | "github.com/modelcontextprotocol/go-sdk/mcp" |
| 19 | ) |
| 20 | |
| 21 | func main() { |
| 22 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) |
| 23 | defer stop() |
| 24 | if err := run(ctx, os.Args[1:], os.Stdout); err != nil { |
| 25 | fmt.Fprintln(os.Stderr, err) |
| 26 | os.Exit(1) |
| 27 | } |
| 28 | } |
| 29 | func browser(raw string) error { |
| 30 | var cmd *exec.Cmd |
| 31 | switch runtime.GOOS { |
| 32 | case "darwin": |
| 33 | cmd = exec.Command("open", raw) |
| 34 | case "windows": |
| 35 | cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", raw) |
| 36 | default: |
| 37 | cmd = exec.Command("xdg-open", raw) |
| 38 | } |
| 39 | return cmd.Run() |
| 40 | } |
| 41 | func run(ctx context.Context, args []string, out io.Writer) error { |
| 42 | if len(args) < 1 { |
| 43 | return errors.New("usage: oficina auth login|status|logout, teams list, tools, call <operation>, <app> <action>, or mcp serve") |
| 44 | } |
| 45 | c := client.New(client.Keyring{}) |
| 46 | print := func(v any) error { enc := json.NewEncoder(out); enc.SetIndent("", " "); return enc.Encode(v) } |
| 47 | switch args[0] { |
| 48 | case "auth": |
| 49 | if len(args) < 2 { |
| 50 | return errors.New("choose auth login, status or logout") |
| 51 | } |
| 52 | switch args[1] { |
| 53 | case "login": |
| 54 | fs := flag.NewFlagSet("auth login", flag.ContinueOnError) |
| 55 | server := fs.String("server", "", "Oficina connection server origin") |
| 56 | if err := fs.Parse(args[2:]); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if *server == "" { |
| 60 | return errors.New("supply --server https://your-oficina-connection-server") |
| 61 | } |
| 62 | ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) |
| 63 | defer cancel() |
| 64 | if err := c.Login(ctx, *server, nil, browser); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | fmt.Fprintln(out, "Connected to Oficina.") |
| 68 | return nil |
| 69 | case "status": |
| 70 | creds, err := c.Store.Load() |
| 71 | if err != nil || creds.Token == nil { |
| 72 | return errors.New("not connected") |
| 73 | } |
| 74 | return print(map[string]any{"server": creds.Server, "expires_at": creds.Token.Expiry}) |
| 75 | case "logout": |
| 76 | if err := c.Logout(ctx); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | fmt.Fprintln(out, "Connection revoked and local credentials removed.") |
| 80 | return nil |
| 81 | } |
| 82 | return errors.New("unknown auth command") |
| 83 | case "teams": |
| 84 | if len(args) != 2 || args[1] != "list" { |
| 85 | return errors.New("usage: oficina teams list") |
| 86 | } |
| 87 | info, err := c.Connection(ctx) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | return print(info.Teams) |
| 92 | case "tools": |
| 93 | ops, err := c.Operations(ctx) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | return print(ops) |
| 98 | case "mcp": |
| 99 | if len(args) != 2 || args[1] != "serve" { |
| 100 | return errors.New("usage: oficina mcp serve") |
| 101 | } |
| 102 | ops, err := c.Operations(ctx) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | s := mcp.NewServer(&mcp.Implementation{Name: "oficina", Version: "0.1.0"}, &mcp.ServerOptions{Instructions: access.AgentInstructions}) |
| 107 | access.AddConnectionTool(s, c.Connection) |
| 108 | for _, op := range ops { |
| 109 | destructive := op.Destructive |
| 110 | s.AddTool(&mcp.Tool{Name: op.Name, Description: op.Description, InputSchema: op.Input, OutputSchema: op.Output, Annotations: &mcp.ToolAnnotations{ReadOnlyHint: op.ReadOnly, DestructiveHint: &destructive, IdempotentHint: op.Idempotent}}, func(ctx context.Context, r *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 111 | result, err := c.Call(ctx, op.Name, r.Params.Arguments) |
| 112 | if err != nil { |
| 113 | return &mcp.CallToolResult{IsError: true, Content: []mcp.Content{&mcp.TextContent{Text: err.Error()}}}, nil |
| 114 | } |
| 115 | raw, _ := json.Marshal(result) |
| 116 | return &mcp.CallToolResult{StructuredContent: result, Content: []mcp.Content{&mcp.TextContent{Text: string(raw)}}}, nil |
| 117 | }) |
| 118 | } |
| 119 | return s.Run(ctx, &mcp.StdioTransport{}) |
| 120 | } |
| 121 | if len(args) < 2 { |
| 122 | return errors.New("supply an operation name or app action") |
| 123 | } |
| 124 | name := args[1] |
| 125 | if args[0] != "call" { |
| 126 | name = args[0] + "_" + args[1] |
| 127 | } |
| 128 | fs := flag.NewFlagSet("call", flag.ContinueOnError) |
| 129 | team := fs.String("team", "", "selected team") |
| 130 | file := fs.String("input", "-", "JSON input file or - for stdin") |
| 131 | if err := fs.Parse(args[2:]); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | var in io.Reader = os.Stdin |
| 135 | if *file != "-" { |
| 136 | f, err := os.Open(*file) |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | defer f.Close() |
| 141 | in = f |
| 142 | } |
| 143 | raw, err := io.ReadAll(io.LimitReader(in, access.MaxPayload+1)) |
| 144 | if err != nil || len(raw) > access.MaxPayload { |
| 145 | return errors.New("could not read operation input within the size limit") |
| 146 | } |
| 147 | if access.UniqueJSON(raw) != nil { |
| 148 | return errors.New("send one JSON object with unique field names") |
| 149 | } |
| 150 | var input map[string]any |
| 151 | if access.DecodeJSON(raw, &input) != nil || input == nil { |
| 152 | return errors.New("send one JSON object") |
| 153 | } |
| 154 | if *team != "" { |
| 155 | if existing, ok := input["team"]; ok && existing != *team { |
| 156 | return errors.New("--team disagrees with the input team") |
| 157 | } |
| 158 | input["team"] = *team |
| 159 | } |
| 160 | raw, err = json.Marshal(input) |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | result, err := c.Call(ctx, name, raw) |
| 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | return print(result) |
| 169 | } |
| 170 | |