{"modulePath":"github.com/txn2/mcp-data-platform","version":"v1.172.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package scripthttp\n\nPackage scripthttp exposes managed scripts over the admin REST API — the script list, a script's version history, one version with what its code reaches for, schedules, and the run history — and the portal's own script routes.\n\nIt lives beside the other version seam (internal/httpserver/versionhttp) rather than inside pkg/admin so that package stays within its size budget; the composition root mounts it under the admin path prefix wrapped in the admin authentication middleware and injects the identity accessor, so this package never imports the admin surface.\n\n## Functions\n\n```go\nfunc PortalIdentityOf(isAdmin func([]string) bool, resolver portal.PersonaResolver) func(r *http.Request) *PortalIdentity\n```\nPortalIdentityOf resolves the portal caller for the script routes: who they are, the persona they resolved to, and whether they hold the admin roles that make the surface unrestricted for them. A request with no signed-in user resolves to nil, which the handlers answer as a 401; a nil resolver leaves the persona empty.\n\n## Types\n\n```go\ntype AuditRecorder interface {\n\tLog(ctx context.Context, event audit.Event) error\n}\n```\nAuditRecorder writes one audit event. It is the write half of audit.Logger: this surface records administrative writes and never reads the log back, and a narrower dependency is one less thing a deployment has to supply to mount these routes.\n\n```go\ntype ContractReader interface {\n\tContract(ctx context.Context, id string) (*script.Contract, error)\n}\n```\nContractReader composes one script's contract document: the record, its parameter contract, the cadence, and the last successful run. It is the same document a reference to a script resolves to (#1302), so the portal page and an agent's fetch describe a script identically.\n\n```go\ntype Deps struct {\n\tScripts  script.Store\n\tVersions script.VersionStore\n\t// Schedules is the cadence store. Nil leaves the schedule routes unmounted,\n\t// which is the honest shape for a deployment that cannot keep a schedule.\n\tSchedules script.ScheduleStore\n\n\t// Runs is the run history. Nil leaves the portal run routes unmounted: a\n\t// deployment that keeps no runs has no history to show.\n\tRuns script.RunStore\n\t// Contracts composes one script's contract document for the portal detail\n\t// route. Nil leaves that route unmounted.\n\tContracts ContractReader\n\t// LatestRuns reports each script's most recent run for the portal listing.\n\t// Nil leaves the listing's last-run column empty rather than unmounting it.\n\tLatestRuns LatestRunReader\n\n\t// States holds each script's one object of state (#1537): what a run\n\t// reads as run.state and saves with platform.save_state. Nil leaves the\n\t// state routes unmounted and a draft reading {}, which is the shape of a\n\t// deployment that keeps no state.\n\tStates script.StateStore\n\n\t// DryRuns records and resolves the accounts of draft executions (#1364).\n\t// Nil runs drafts without keeping an account of them, which leaves every\n\t// version reading as one nobody dry-ran — the state before the account\n\t// existed — rather than failing the run.\n\tDryRuns script.DryRunStore\n\n\t// Gate is what every source edit crosses (#1939, #1940, #1942): the lint,\n\t// the script's tests and what the new version does differently. Nil\n\t// checks the lint and the tests with no recording readable.\n\tGate *scriptsave.Gate\n\n\t// Drafts executes a draft under the calling person's own identity. Nil\n\t// leaves the dry-run route unmounted; validate needs nothing but the\n\t// source and stays either way.\n\tDrafts DraftRunner\n\n\t// Destinations is the deployment's declared bucket destination set, which\n\t// validate and dry-run check a source's named destinations against so an\n\t// undeclared one is reported before the script's queries run (#1415).\n\t// Empty means the portal is the only place a script may write, which is\n\t// what the runtime already reports.\n\tDestinations []script.Destination\n\n\t// Connections enumerates the connections the portal caller's own persona\n\t// reaches, which a connection-typed parameter is chosen from (#1361). Nil\n\t// leaves the choices route unmounted.\n\tConnections connchoicehttp.Enumerator\n\n\t// Audit records administrative script writes — today the owner transfer\n\t// (#1404). Nil leaves the transfer working and unrecorded, which is what a\n\t// deployment with no audit store has for every other write too.\n\tAudit AuditRecorder\n\n\t// AdminEmail returns the authenticated administrator's email, recorded as\n\t// the author of an admin-made edit.\n\tAdminEmail func(r *http.Request) string\n\t// PortalUser resolves the authenticated portal caller, or nil when the\n\t// request carries no user. Nil leaves the portal routes unmounted, which is\n\t// what the admin surface passes.\n\tPortalUser func(r *http.Request) *PortalIdentity\n\n\t// ContentURL mints a signed link to one asset version (#1848), which the\n\t// run-outputs listing hands out. Nil lists outputs without links.\n\tContentURL func(assetID string, version int) (string, time.Time)\n\n\t// Grants keeps who other than the owner may run each script (#1846). Nil\n\t// leaves the grant routes unmounted and every run the owner's.\n\tGrants scriptgrant.Store\n\t// Handlers carries a script's handlers out (#2441): its endpoints,\n\t// schedules and webhook sources. Nil leaves the handler routes\n\t// unmounted.\n\tHandlers *scripthandler.Service\n\t// CallTester answers a connection's test panel (#2436); nil has none.\n\tCallTester handlerhttp.Tester\n\n\t// Produced lists everything a script has written across every run (#1569).\n\t// Nil leaves that route unmounted, which is the shape of a deployment that\n\t// records no producers.\n\tProduced ProducedReader\n}\n```\nDeps carries the collaborators the handlers need. Every store except Schedules is required; a deployment without them does not mount these routes.\n\n```go\ntype DraftRunner interface {\n\tRun(ctx context.Context, req scriptdraft.Request) (*scriptdraft.Outcome, error)\n}\n```\nDraftRunner executes a draft under the calling person's own identity. The composition root supplies it, because building one needs the assembled MCP server. Nil leaves the dry-run route unmounted; validate stays, since parsing needs nothing but the source.\n\n```go\ntype Handler struct {\n\t// contains filtered or unexported fields\n}\n```\nHandler serves the script routes.\n\n```go\nfunc New(deps Deps) *Handler\n```\nNew builds the handler.\n\n```go\nfunc (h *Handler) ActsOnScript(r *http.Request, user *PortalIdentity, id string) bool\n```\nActsOnScript reports whether the caller owns the script or administers the platform: the readers of what acting on it shows, such as the arguments its run's calls were sent (#1982). A script that cannot be read answers false.\n\n```go\nfunc (h *Handler) EditOwned(w http.ResponseWriter, r *http.Request, mutate func(*script.Script)) (*script.Script, bool)\n```\nEditOwned applies mutate to the script in the path for its owner or an administrator, through the edit funnel, writing every refusal itself.\n\n```go\nfunc (h *Handler) LoadScriptVersion(w http.ResponseWriter, r *http.Request) (*script.Script, *script.Version, bool)\n```\nLoadScriptVersion resolves both the script and the version named by the path. The version alone does not answer what the script is executing today, which is what a review is read against.\n\n```go\nfunc (h *Handler) OwnedByCaller(w http.ResponseWriter, r *http.Request) (scriptID, actor string, ok bool)\n```\nOwnedByCaller is ownedScript for a route mounted outside this package (#2180): it resolves the caller too, answering 401 when there is none, and reports the script's id and the caller's identity.\n\n```go\nfunc (h *Handler) ReadableRun(w http.ResponseWriter, r *http.Request, user *PortalIdentity) (*script.Run, bool)\n```\nReadableRun reads the run in the path for a caller entitled to all of it, or writes the refusal: its cancel and its drawing on the flow diagram, which shows the calls it made. A run id is unguessable, but unguessable is not an authorization rule: the run must belong to the script in the path, and the caller must be the script's owner, an administrator, or whoever asked for this particular run -- the result was handed to them when they requested it, so a run they cannot re-read is an id they cannot follow. Reading and canceling a run answer to the same rule (#1847).\n\n```go\nfunc (h *Handler) RegisterAdmin(mux *http.ServeMux, prefix string, wrap func(http.Handler) http.Handler)\n```\nRegisterAdmin mounts the admin script routes under prefix, each wrapped in the admin authentication middleware.\n\n```go\nfunc (h *Handler) RegisterPortal(mux *http.ServeMux, wrap func(http.Handler) http.Handler)\n```\nRegisterPortal mounts the portal read routes, wrapped in the portal authentication middleware. Every handler goes through portalHandler, which resolves the caller and answers 401 once for all of them.\n\nThe run routes are mounted only where the deployment keeps runs, the detail route only where a contract can be composed, and the schedule routes only where the deployment keeps schedules; a deployment missing any of them serves the rest rather than failing per request.\n\n```go\ntype LatestRunReader interface {\n\tLatestRuns(ctx context.Context, scriptIDs []string) (map[string]script.Run, error)\n}\n```\nLatestRunReader returns the most recent run of each named script, keyed by script id and omitting the scripts that have never run. It is a listing capability rather than a history one: the alternative is one query per row.\n\n```go\ntype PortalIdentity struct {\n\tUserID  string\n\tEmail   string\n\tPersona string\n\tIsAdmin bool\n\t// Roles is the authority this caller holds. It is recorded on any version\n\t// they author (#1307): a run of that version presents exactly these roles,\n\t// which is what keeps a script from holding more access than the person\n\t// who wrote it.\n\tRoles []string\n\t// AuthType is HOW this caller was authenticated. It matters because the\n\t// dry-run route opens a session on their behalf (#1364): that session must\n\t// present the authentication the request actually arrived with rather than\n\t// a kind no authenticator issues.\n\tAuthType string\n\t// Claims are the caller's claims, an API key's attributes among them: a\n\t// parameter bound to caller.\u003cclaim\u003e takes its value from here (#1846).\n\tClaims map[string]any\n}\n```\nPortalIdentity is the portal caller, resolved by the accessor the composition root injects. IsAdmin carries the administrator's unrestricted reach into this surface: an admin sees every script and every run, which is the same authority the admin API already gives them.\n\n```go\ntype ProducedReader interface {\n\tProduced(ctx context.Context, scriptID string, limit int) ([]producedview.Item, error)\n}\n```\nProducedReader lists what one script has produced, most recently written first, resolving each file to its current name. limit is the cap on rows, non-positive selecting the reader's own default.\n\n","path":"github.com/txn2/mcp-data-platform/internal/httpserver/scripthttp","name":"scripthttp","synopsis":"Package scripthttp exposes managed scripts over the admin REST API — the script list, a script's version history, one version with what its code reaches for, schedules, and the run history — and the portal's own script routes.","isRedistributable":true}
