Guide

Plain HTTP

Serve the same tools as JSON over plain HTTP. One GET lists them, one POST calls one, and writes stay off until you say so

A GTK app, script or dashboard can call your tools with a plain POST and get JSON back. Making a GTK app learn an MCP handshake just to call a tool is overkill. Want to give it a simpler door?

Mount the handler

Import createHttpHandler from @agntn/tools/http. Pass it info, the tools and options. info is just a name and a version.

You get a Fetch handler with a fetch(request) method. It takes a Request and returns a Response. Any host that speaks Fetch can mount it. Here, h3 mounts it at /http/slugs:

tsserver.ts
import { H3, serve } from "h3";
import { createHttpHandler } from "@agntn/tools/http";
import { slugTool } from "./tools.ts";

const http = createHttpHandler({ name: "slugs", version: "1.0.0" }, [slugTool], {
  base: "/http/slugs",
});

const app = new H3().all("/http/slugs/**", (event) => http.fetch(event.req));
serve(app, { port: 3000 });

List the tools

A GET at the root lists the server name, version and tools. Each tool has a name, title, description, effect and inputSchema. With base set to /http/slugs, the listing lives there:

textterminal
$ curl -s http://localhost:3000/http/slugs | jq
{
  "name": "slugs",
  "version": "1.0.0",
  "tools": [
    {
      "name": "text_slug",
      "title": "Text Slug",
      "description": "Turn a title into a lowercase URL slug.",
      "effect": "read",
      "inputSchema": {
        "type": "object",
        "required": [
          "text"
        ],
        "properties": {
          "text": {
            "type": "string",
            "minLength": 1
          }
        },
        "additionalProperties": false
      }
    }
  ]
}

Call a tool

POST to the tool's name under the base path. Send its input as JSON. An empty body counts as {}.

textterminal
$ curl -s -H 'content-type: application/json' -d '{"text":"Hello, World"}' http://localhost:3000/http/slugs/text_slug
{"text":"hello-world","details":{"slug":"hello-world"}}

The core checks input before calling the tool, just as it does on other hosts. A typo gets the same error lines MCP would return:

textterminal
$ curl -s -w '\n%{http_code}\n' -H 'content-type: application/json' -d '{"txt":"Hello, World"}' http://localhost:3000/http/slugs/text_slug
{"lines":["Invalid arguments: unknown property \"txt\"; takes text","Invalid arguments at /: must have required properties text"]}
400

What comes back?

A tool answer has text and details. It's 200, or 422 if the tool set isError. Both fields stay in the answer. Results with image blocks also include images, an array of data and mimeType objects.

A refusal has lines instead. Each line is a separate entry. Newlines inside an error message become spaces, so they can't forge another line.

StatusBodyWhen
200{ text, details }the tool answered
422{ text, details }the tool set isError
400{ lines }bad input, or a body that isn't JSON
403{ lines }the tool's effect isn't in effects
404{ lines }no such tool, or nothing at that path
405{ lines }wrong method, Allow names the right one
413{ lines }body over maxRequestBodySize, 4 MiB by default
415{ lines }no Content-Type: application/json
500{ lines }the tool threw, caused by: lines included

The body needs Content-Type: application/json, or the handler returns 415. A browser page can't send that header across origins without a CORS preflight. This handler never answers one.

Writes stay off

The effects option defaults to ["read"]. An HTTP call has nobody there to click "approve". Write tools stay out of the listing and return 403.

You can turn writes on:

tsserver.ts
const http = createHttpHandler({ name: "slugs", version: "1.0.0" }, [slugTool], {
  base: "/http/slugs",
  effects: ["read", "write"],
});

Only do that behind your host's authentication.

Stream progress over HTTP

Want to see a call make progress? Send your usual POST with Accept: text/event-stream. You'll get Server-Sent Events with Content-Type: text/event-stream and Cache-Control: no-cache.

Each progress event carries message, plus progress and total when they're numbers. HTTP doesn't renumber or drop updates. That's handy for a slow call: you can tell it's still working.

Here a job_wait tool, mounted at /http/jobs, checks a build three times:

textterminal
$ curl -sN -H 'accept: text/event-stream' -H 'content-type: application/json' -d '{"id":"build-42"}' http://localhost:3000/http/jobs/job_wait
event: progress
data: {"message":"build-42 still running, check 1","progress":1,"total":3}

event: progress
data: {"message":"build-42 still running, check 2","progress":2,"total":3}

event: progress
data: {"message":"build-42 still running, check 3","progress":3,"total":3}

event: result
data: {"status":200,"text":"build-42 is done","details":{"id":"build-42","checks":3}}

The stream ends with one result event. It carries the usual { text, details } or { lines }, plus status. The status rides in the result because the headers already left with the first event. Close the stream halfway and the read stops with it.

Bad tool input arrives as a result with status: 400 and the usual lines.

textterminal
$ curl -sN -H 'accept: text/event-stream' -H 'content-type: application/json' -d '{"di":"build-42"}' http://localhost:3000/http/jobs/job_wait
event: result
data: {"status":400,"lines":["Invalid arguments: unknown property \"di\"; takes id","Invalid arguments at /: must have required properties id"]}

Some refusals happen before the call starts. These stay plain JSON, even when you ask for events:

  • Unknown tool: 404.
  • Wrong method: 405.
  • Write tool not served: 403.
  • Missing JSON content type: 415.
  • Oversized body: 413.
  • Body that isn't JSON: 400.

Without the header, you'll get one JSON answer at the end. Accept: */* doesn't request events. Neither does text/event-stream;q=0.

Who's calling?

Authentication belongs to the host. Check Host, Origin and a token before the request reaches the handler.

Tools receive host with http: true and the request, so they can read its headers. But client headers are name tags, not keys. They can identify a caller, not authorize one.

The request's signal becomes the call's signal. If the host aborts it when the client hangs up, a read is cancelled. h3 does. Progress is available only on a streamed call, which makes a long wait easier to follow. But ask is absent: an HTTP form has nobody to fill it in.