mcp_client

package
v0.14.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package mcp_client is a minimal MCP Streamable HTTP client used by the ailang CLI to fetch fresh content (prompts, docs search, stdlib) from mcp.ailang.sunholo.com when --source=mcp or --source=auto is in effect.

Goals:

  • Tiny surface: just enough to call one tool per CLI command
  • Version-locked: every call passes for_version=<CLI's compile-time version> and the response's served_for is checked. Mismatch -> ErrVersionMismatch so the caller falls back silently to embedded.
  • Bounded latency: 1.5s default timeout, configurable via env
  • Stateless: opens + closes one MCP session per call. Caching is the caller's responsibility (see internal/prompt for the on-disk cache).

Usage:

c := mcp_client.New(mcp_client.Options{
    BaseURL:        os.Getenv("AILANG_MCP_URL"),  // empty -> default prod
    AILangVersion:  version.Version,
    Timeout:        1500 * time.Millisecond,
})
out, err := c.CallTool(ctx, "prompt_get", map[string]any{
    "forVersion": c.AILangVersion,
    "kind":       "agent",
})

Index

Constants

View Source
const DefaultTimeout = 1500 * time.Millisecond

DefaultTimeout caps a single MCP round-trip. Network failures should not stall an interactive `ailang prompt` command; the embedded fallback wins after this elapses.

View Source
const DefaultURL = "https://mcp.ailang.sunholo.com/mcp/"

DefaultURL is the canonical MCP endpoint for ailang.sunholo.com.

View Source
const ProtocolVersion = "2024-11-05"

ProtocolVersion is the MCP wire version we negotiate during initialize.

Variables

View Source
var ErrVersionMismatch = errors.New("mcp_client: server has no content for this AILANG version")

ErrVersionMismatch means the server returned content tagged for a different AILANG version (typically because the snapshot doesn't have content for the caller's version). Callers should silently fall back to embedded.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a minimal MCP Streamable HTTP client.

func New

func New(opts Options) *Client

New constructs a Client. It does NOT open a session — sessions are per-call.

func (*Client) AILangVersion

func (c *Client) AILangVersion() string

AILangVersion returns the CLI version this client was constructed with.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the configured MCP endpoint (without trailing slash).

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, toolName string, args map[string]any) (map[string]any, error)

CallTool runs initialize -> notifications/initialized -> tools/call against the MCP endpoint and returns the parsed response payload.

If the response is an envelope of the form {served_for, data, ...} (which every version-scoped MCP tool returns) AND served_for != AILangVersion AND AILangVersion was passed in args under "forVersion", the call returns ErrVersionMismatch so the caller can silently fall back to embedded.

If the tool returned an error envelope ({error, detail}), the call returns *ErrToolError with the parsed code/detail.

type ErrToolError

type ErrToolError struct {
	Code   string
	Detail string
	Body   string
}

ErrToolError means the tool call returned an MCP error envelope (isError=true) or a structured {error: ...} JSON body. The body is preserved in the error.

func (*ErrToolError) Error

func (e *ErrToolError) Error() string

type Options

type Options struct {
	// BaseURL is the MCP endpoint. Empty resolves to DefaultURL.
	BaseURL string
	// AILangVersion is the CLI's compile-time version, passed as for_version
	// in every tool call.
	AILangVersion string
	// Timeout is per-request. Zero resolves to DefaultTimeout.
	Timeout time.Duration
	// HTTPClient lets tests inject a fake transport.
	HTTPClient *http.Client
}

Options bundles the CLI-side config surface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL