Add CLI client subcommands and MCP server
Adds push, list, get, delete, and login subcommands backed by an HTTP API client, plus an MCP server for tool-based access to the document queue. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
223
internal/client/client.go
Normal file
223
internal/client/client.go
Normal file
@@ -0,0 +1,223 @@
|
||||
// Package client provides an HTTP client for the MCQ REST API.
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrForbidden = errors.New("forbidden")
|
||||
)
|
||||
|
||||
// Document represents a document returned by the MCQ API.
|
||||
type Document struct {
|
||||
ID int64 `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
PushedBy string `json:"pushed_by"`
|
||||
PushedAt string `json:"pushed_at"`
|
||||
Read bool `json:"read"`
|
||||
}
|
||||
|
||||
// Client talks to a remote MCQ server's REST API.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
token string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// Option configures a Client.
|
||||
type Option func(*Client)
|
||||
|
||||
// WithHTTPClient sets a custom HTTP client (useful for testing).
|
||||
func WithHTTPClient(hc *http.Client) Option {
|
||||
return func(c *Client) {
|
||||
c.httpClient = hc
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a Client. baseURL is the MCQ server URL (e.g. "https://mcq.example.com:8443").
|
||||
// token is a Bearer token from MCIAS login.
|
||||
func New(baseURL, token string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
token: token,
|
||||
httpClient: http.DefaultClient,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(c)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) do(ctx context.Context, method, path string, body any) (*http.Response, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
buf, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
bodyReader = bytes.NewReader(buf)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized:
|
||||
_ = resp.Body.Close()
|
||||
return nil, ErrUnauthorized
|
||||
case http.StatusForbidden:
|
||||
_ = resp.Body.Close()
|
||||
return nil, ErrForbidden
|
||||
case http.StatusNotFound:
|
||||
_ = resp.Body.Close()
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func decodeJSON[T any](resp *http.Response) (T, error) {
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
var v T
|
||||
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
|
||||
return v, fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Login authenticates with MCIAS and returns a bearer token.
|
||||
func (c *Client) Login(ctx context.Context, username, password, totpCode string) (string, error) {
|
||||
resp, err := c.do(ctx, http.MethodPost, "/v1/auth/login", map[string]string{
|
||||
"username": username,
|
||||
"password": password,
|
||||
"totp_code": totpCode,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result, err := decodeJSON[struct {
|
||||
Token string `json:"token"`
|
||||
}](resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.Token, nil
|
||||
}
|
||||
|
||||
// ListDocuments returns all documents in the queue.
|
||||
func (c *Client) ListDocuments(ctx context.Context) ([]Document, error) {
|
||||
resp, err := c.do(ctx, http.MethodGet, "/v1/documents", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := decodeJSON[struct {
|
||||
Documents []Document `json:"documents"`
|
||||
}](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Documents, nil
|
||||
}
|
||||
|
||||
// GetDocument fetches a single document by slug.
|
||||
func (c *Client) GetDocument(ctx context.Context, slug string) (*Document, error) {
|
||||
resp, err := c.do(ctx, http.MethodGet, "/v1/documents/"+slug, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc, err := decodeJSON[Document](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// PutDocument creates or replaces a document.
|
||||
func (c *Client) PutDocument(ctx context.Context, slug, title, body string) (*Document, error) {
|
||||
resp, err := c.do(ctx, http.MethodPut, "/v1/documents/"+slug, map[string]string{
|
||||
"title": title,
|
||||
"body": body,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc, err := decodeJSON[Document](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// DeleteDocument removes a document by slug.
|
||||
func (c *Client) DeleteDocument(ctx context.Context, slug string) error {
|
||||
resp, err := c.do(ctx, http.MethodDelete, "/v1/documents/"+slug, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkRead marks a document as read.
|
||||
func (c *Client) MarkRead(ctx context.Context, slug string) (*Document, error) {
|
||||
resp, err := c.do(ctx, http.MethodPost, "/v1/documents/"+slug+"/read", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc, err := decodeJSON[Document](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// MarkUnread marks a document as unread.
|
||||
func (c *Client) MarkUnread(ctx context.Context, slug string) (*Document, error) {
|
||||
resp, err := c.do(ctx, http.MethodPost, "/v1/documents/"+slug+"/unread", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc, err := decodeJSON[Document](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
var h1Re = regexp.MustCompile(`(?m)^#\s+(.+)$`)
|
||||
|
||||
// ExtractTitle returns the first H1 heading from markdown source.
|
||||
// If no H1 is found, it returns the fallback string.
|
||||
func ExtractTitle(markdown, fallback string) string {
|
||||
m := h1Re.FindStringSubmatch(markdown)
|
||||
if m == nil {
|
||||
return fallback
|
||||
}
|
||||
return strings.TrimSpace(m[1])
|
||||
}
|
||||
239
internal/client/client_test.go
Normal file
239
internal/client/client_test.go
Normal file
@@ -0,0 +1,239 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testServer(t *testing.T) (*httptest.Server, *Client) {
|
||||
t.Helper()
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("POST /v1/auth/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
if req.Username == "admin" && req.Password == "pass" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"token": "tok123"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /v1/documents", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"documents": []Document{
|
||||
{ID: 1, Slug: "test-doc", Title: "Test", Body: "# Test\nHello", PushedBy: "admin", PushedAt: "2026-01-01T00:00:00Z", Read: false},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
if slug == "missing" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Body: "# Test\nHello"})
|
||||
})
|
||||
|
||||
mux.HandleFunc("PUT /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
var req struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: req.Title, Body: req.Body, PushedBy: "admin"})
|
||||
})
|
||||
|
||||
mux.HandleFunc("DELETE /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
if slug == "missing" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /v1/documents/{slug}/read", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Read: true})
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /v1/documents/{slug}/unread", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Read: false})
|
||||
})
|
||||
|
||||
ts := httptest.NewServer(mux)
|
||||
t.Cleanup(ts.Close)
|
||||
c := New(ts.URL, "tok123", WithHTTPClient(ts.Client()))
|
||||
return ts, c
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
c.token = "" // login doesn't need a pre-existing token
|
||||
|
||||
token, err := c.Login(context.Background(), "admin", "pass", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Login: %v", err)
|
||||
}
|
||||
if token != "tok123" {
|
||||
t.Fatalf("got token %q, want %q", token, "tok123")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginBadCredentials(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
c.token = ""
|
||||
|
||||
_, err := c.Login(context.Background(), "admin", "wrong", "")
|
||||
if err != ErrUnauthorized {
|
||||
t.Fatalf("got %v, want ErrUnauthorized", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDocuments(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
docs, err := c.ListDocuments(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("ListDocuments: %v", err)
|
||||
}
|
||||
if len(docs) != 1 {
|
||||
t.Fatalf("got %d docs, want 1", len(docs))
|
||||
}
|
||||
if docs[0].Slug != "test-doc" {
|
||||
t.Fatalf("got slug %q, want %q", docs[0].Slug, "test-doc")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDocument(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
doc, err := c.GetDocument(context.Background(), "test-doc")
|
||||
if err != nil {
|
||||
t.Fatalf("GetDocument: %v", err)
|
||||
}
|
||||
if doc.Slug != "test-doc" {
|
||||
t.Fatalf("got slug %q, want %q", doc.Slug, "test-doc")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDocumentNotFound(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
_, err := c.GetDocument(context.Background(), "missing")
|
||||
if err != ErrNotFound {
|
||||
t.Fatalf("got %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutDocument(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
doc, err := c.PutDocument(context.Background(), "new-doc", "New Doc", "# New\nContent")
|
||||
if err != nil {
|
||||
t.Fatalf("PutDocument: %v", err)
|
||||
}
|
||||
if doc.Slug != "new-doc" {
|
||||
t.Fatalf("got slug %q, want %q", doc.Slug, "new-doc")
|
||||
}
|
||||
if doc.Title != "New Doc" {
|
||||
t.Fatalf("got title %q, want %q", doc.Title, "New Doc")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDocument(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
if err := c.DeleteDocument(context.Background(), "test-doc"); err != nil {
|
||||
t.Fatalf("DeleteDocument: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDocumentNotFound(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
err := c.DeleteDocument(context.Background(), "missing")
|
||||
if err != ErrNotFound {
|
||||
t.Fatalf("got %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkRead(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
doc, err := c.MarkRead(context.Background(), "test-doc")
|
||||
if err != nil {
|
||||
t.Fatalf("MarkRead: %v", err)
|
||||
}
|
||||
if !doc.Read {
|
||||
t.Fatal("expected doc to be marked read")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkUnread(t *testing.T) {
|
||||
_, c := testServer(t)
|
||||
|
||||
doc, err := c.MarkUnread(context.Background(), "test-doc")
|
||||
if err != nil {
|
||||
t.Fatalf("MarkUnread: %v", err)
|
||||
}
|
||||
if doc.Read {
|
||||
t.Fatal("expected doc to be marked unread")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractTitle(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
markdown string
|
||||
fallback string
|
||||
want string
|
||||
}{
|
||||
{"h1 found", "# My Title\nSome content", "default", "My Title"},
|
||||
{"no h1", "Some content without heading", "default", "default"},
|
||||
{"h2 not matched", "## Subtitle\nContent", "default", "default"},
|
||||
{"h1 with spaces", "# Spaced Title \nContent", "default", "Spaced Title"},
|
||||
{"multiple h1s", "# First\n# Second", "default", "First"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ExtractTitle(tt.markdown, tt.fallback)
|
||||
if got != tt.want {
|
||||
t.Errorf("ExtractTitle() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBearerTokenSent(t *testing.T) {
|
||||
var gotAuth string
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"documents": []Document{}})
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
c := New(ts.URL, "my-secret-token", WithHTTPClient(ts.Client()))
|
||||
_, _ = c.ListDocuments(context.Background())
|
||||
|
||||
if gotAuth != "Bearer my-secret-token" {
|
||||
t.Fatalf("got Authorization %q, want %q", gotAuth, "Bearer my-secret-token")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user