Help us shape the future of Go! Take the 2026 Go Developer Survey

calltask

package
v1.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package calltask executes a tool call apart from the request that asked for it, as an MCP task (#2009).

A client that declares the Tasks extension and calls an export tool (trino_export, api_export, graphql_export) is answered at once with a task handle, and the export runs on the replica that took the call, on a context that outlives the request. The task is a call_tasks row: who started it, where it stands, and once it ends the tool's result or the JSON-RPC error it failed with, so tasks/get is answered by any replica.

While a call runs, its replica stamps the row's heartbeat every few seconds and reads, in the same statement, whether a cancel was asked for: that is how a tasks/cancel another replica took reaches the call. A row whose heartbeat stopped belongs to a replica that is gone, and is read as failed.

The runner sits inside the tools/call chain where mcptask.Middleware hands it a call, so authorization, the gates and the consent prompts have already answered; what runs apart is the rest of the chain and the tool.

Index

Constants

View Source
const (
	// DefaultHeartbeat is how often a replica stamps the calls it runs.
	DefaultHeartbeat = 2 * time.Second

	// DefaultPollInterval is how often a client is asked to read a task.
	DefaultPollInterval = 2 * time.Second
	// DefaultRetention is how long a task is kept after it is created.
	DefaultRetention = 7 * 24 * time.Hour
)

Timing defaults.

View Source
const IDPrefix = "dpt_"

IDPrefix marks a call task's id.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ending

type Ending struct {
	Status  string
	Message string
	Result  json.RawMessage
	Error   json.RawMessage
}

Ending is how a call ended: its terminal status, the message the task carries, and the tool result or the JSON-RPC error, in wire form.

type Exports added in v1.149.0

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

Exports is the runner as the export tools start and find background exports through (#2212): the same call tasks, created for a client that does not declare the Tasks extension, marked as background exports and found again by their idempotency key.

func (*Exports) Ended added in v1.149.0

func (e *Exports) Ended(ctx context.Context, userID string, since time.Time, limit int) ([]exportasync.Run, error)

Ended reads the caller's background exports that ended after since, newest first, at most limit: what platform_info tells its caller (#2212).

func (*Exports) Find added in v1.149.0

func (e *Exports) Find(ctx context.Context, tool, key string) (*exportasync.Run, error)

Find reads the caller's latest background export of tool under key. A working one whose replica stopped reporting is failed first.

func (*Exports) Start added in v1.149.0

func (e *Exports) Start(
	ctx context.Context, tool, key string, work func(ctx context.Context) (*mcp.CallToolResult, error),
) (*exportasync.Run, bool, error)

Start runs work as a background export under key, or answers with the one already working under it for this caller and tool.

type Options

type Options struct {
	Store Store
	// Retention is how long a task is kept after it is created; zero is
	// DefaultRetention.
	Retention time.Duration
	// Heartbeat is how often running calls are stamped; zero is
	// DefaultHeartbeat.
	Heartbeat time.Duration
	// PollInterval is the polling interval a client is handed; zero is
	// DefaultPollInterval.
	PollInterval time.Duration
	// Now is the clock; nil is time.Now.
	Now func() time.Time
}

Options configure a Runner.

type PostgresStore

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

PostgresStore is the Store over the call_tasks table.

func NewPostgresStore

func NewPostgresStore(db *sql.DB) *PostgresStore

NewPostgresStore returns the Store over db.

func (*PostgresStore) Create

func (s *PostgresStore) Create(ctx context.Context, t Task) error

Create inserts a working task.

func (*PostgresStore) CreateBackground added in v1.149.0

func (s *PostgresStore) CreateBackground(ctx context.Context, t Task) (*Task, bool, error)

CreateBackground inserts a working background export, or returns the one already working under its caller, tool and key.

func (*PostgresStore) EndedBackground added in v1.149.0

func (s *PostgresStore) EndedBackground(ctx context.Context, userID string, since time.Time, limit int) ([]Task, error)

EndedBackground reads the caller's background exports that ended after since.

func (*PostgresStore) FailStale

func (s *PostgresStore) FailStale(ctx context.Context, id string, before time.Time, message string, rpcErr json.RawMessage) (bool, error)

FailStale fails a working task whose replica stopped reporting.

func (*PostgresStore) FindByKey added in v1.149.0

func (s *PostgresStore) FindByKey(ctx context.Context, userID, tool, key string) (*Task, error)

FindByKey reads the caller's latest task of tool under key.

func (*PostgresStore) Finish

func (s *PostgresStore) Finish(ctx context.Context, id string, end Ending) error

Finish ends a working task.

func (*PostgresStore) Get

func (s *PostgresStore) Get(ctx context.Context, id string) (*Task, error)

Get reads one task.

func (*PostgresStore) Heartbeat

func (s *PostgresStore) Heartbeat(ctx context.Context, ids []string) ([]string, error)

Heartbeat stamps working tasks as alive and returns the ones to cancel.

func (*PostgresStore) RequestCancel

func (s *PostgresStore) RequestCancel(ctx context.Context, id string) error

RequestCancel records a request to stop a working task.

func (*PostgresStore) SetProgress added in v1.149.0

func (s *PostgresStore) SetProgress(ctx context.Context, id string, progress json.RawMessage) error

SetProgress records how far a working task has come.

type Runner

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

Runner executes calls as tasks on this replica and answers for the tasks every replica keeps. It is both the mcptask.Detacher and the mcptask.Source for its tasks.

func New

func New(opts Options) *Runner

New builds a Runner over opts.Store.

func (*Runner) Cancel

func (r *Runner) Cancel(ctx context.Context, id string) error

Cancel asks a task's call to stop: at once when this replica runs it, at the running replica's next heartbeat otherwise.

func (*Runner) Drain

func (r *Runner) Drain(ctx context.Context) error

Drain waits for the calls this replica runs to finish, or for ctx to end, and takes no new ones. It is the shutdown drain: a call a client follows as a task finishes rather than being cut off.

func (*Runner) Exports added in v1.149.0

func (r *Runner) Exports() *Exports

Exports is the runner's background-export face.

func (*Runner) Get

func (r *Runner) Get(ctx context.Context, id string) (*mcptask.Snapshot, error)

Get reports a task to whoever started it or an administrator. A working task whose replica stopped reporting is failed first.

func (*Runner) Prefix

func (*Runner) Prefix() string

Prefix is the call task id prefix.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context, toolName string, run func(ctx context.Context) (mcp.Result, error)) (*mcptask.Snapshot, error)

Start creates the call's task and runs the call apart from the request. A call with no identified caller, which nobody could follow, and one that arrives while the replica shuts down, are declined and answered directly.

func (*Runner) StartHeartbeat

func (r *Runner) StartHeartbeat()

StartHeartbeat begins the heartbeat loop. A second call does nothing.

func (*Runner) Stop

func (r *Runner) Stop(ctx context.Context)

Stop cancels the calls still running, each of which records that its server shut down, waits within ctx for them to record it, and stops the heartbeat. A call that does not return when canceled is left to the process's exit; its task then reads as failed once its heartbeat stops.

type Store

type Store interface {
	// Create inserts a working task.
	Create(ctx context.Context, t Task) error
	// Get reads one task, or errTaskNotFound.
	Get(ctx context.Context, id string) (*Task, error)
	// Finish ends a working task; a task that already ended is left as it
	// is.
	Finish(ctx context.Context, id string, end Ending) error
	// RequestCancel records a request to stop a working task.
	RequestCancel(ctx context.Context, id string) error
	// Heartbeat stamps the working tasks ids name as alive, and returns the
	// ones among them a cancel was requested for.
	Heartbeat(ctx context.Context, ids []string) ([]string, error)
	// FailStale fails a working task whose heartbeat is older than before,
	// reporting whether it did.
	FailStale(ctx context.Context, id string, before time.Time, message string, rpcErr json.RawMessage) (bool, error)
	// CreateBackground inserts a working background export, or returns the
	// one already working for its caller, tool and key with created false.
	CreateBackground(ctx context.Context, t Task) (existing *Task, created bool, err error)
	// FindByKey reads the caller's latest task of tool under key, or
	// errTaskNotFound.
	FindByKey(ctx context.Context, userID, tool, key string) (*Task, error)
	// SetProgress records how far a working task has come.
	SetProgress(ctx context.Context, id string, progress json.RawMessage) error
	// EndedBackground reads the caller's background exports that ended after
	// since, newest first, at most limit.
	EndedBackground(ctx context.Context, userID string, since time.Time, limit int) ([]Task, error)
}

Store is the call_tasks persistence the runner and the Source use.

type Task

type Task struct {
	ID            string
	ToolName      string
	UserID        string
	Status        string
	StatusMessage string
	// Result is the tool's result in its wire form, on a completed task.
	Result json.RawMessage
	// Error is the JSON-RPC error, on a failed task.
	Error           json.RawMessage
	CreatedAt       time.Time
	UpdatedAt       time.Time
	HeartbeatAt     time.Time
	CancelRequested bool
	ExpiresAt       time.Time
	// IdempotencyKey, Background and Progress are a background export's
	// (#2212): the key its caller reads it again by, the mark that it is one,
	// and how far its walk has come.
	IdempotencyKey string
	Background     bool
	Progress       json.RawMessage
}

Task is one call_tasks row.

Jump to

Keyboard shortcuts

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