Search by

atwx / silverstripe-restapi

adiwidjaja

JSON APIs and MCP endpoints for Silverstripe apps: hashed personal API tokens, bearer auth, a JSON controller base and an MCP server.

Package info

github.com/atwx/silverstripe-restapi

Type:silverstripe-vendormodule

pkg:composer/atwx/silverstripe-restapi

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-01 14:35 UTC

This package is auto-updated.

Last update: 2026-11-23 17:14:59 UTC


README

Building blocks for JSON APIs and MCP endpoints in Silverstripe 6 apps:

  • ApiToken: personal access tokens. Only a SHA-256 hash is stored, the plain token (<prefix><40 hex>) is shown once. Optional scopes and expiry.
  • ApiAuth: bearer authentication; logs in the token's member for the request.
  • ApiController: abstract JSON controller (errors {error, fields?}, pagination {items, total, limit, offset}, JSON body, validation → 422).
  • FindsRecords: load records by ID, invisible ones are reported as missing.
  • Mcp\McpController: Model Context Protocol endpoint (Streamable HTTP, JSON responses) with tools from a ToolProvider.
  • sake tasks:create-api-token --email=… --label=… [--scopes=…] [--expires=…] [--tenant=…]

With atwx/silverstripe-tenancy installed, tokens are bound to a tenant: valid only while the member may access it, the tenant is resolved from the token (ApiTokenStrategy, appended to the resolver strategies), and a token is rejected in any other tenant. Issue such tokens with TenantTokens::issue($member, $tenant, $label).

Usage

class NotesApiController extends \Atwx\RestApi\Control\ApiController
{
    use \Atwx\RestApi\FindsRecords;

    private static $url_handlers = ['$ID' => 'handleNote'];
    private static $allowed_actions = ['handleNote'];

    public function handleNote(HTTPRequest $request): HTTPResponse
    {
        $note = $this->findVisible(Note::class, $request->param('ID'));
        if ($request->isPATCH()) {
            $this->requireScope('write');
            // …
        }
        return $this->json(['id' => $note->ID, 'title' => $note->Title]);
    }
}
SilverStripe\Control\Director:
  rules:
    'api/v1/notes': 'App\Api\NotesApiController'
    'mcp': 'Atwx\RestApi\Mcp\McpController'

Atwx\RestApi\Model\ApiToken:
  prefix: 'app_'            # default 'tok_'

Atwx\RestApi\Mcp\McpController:
  server_name: 'myapp'
  instructions: 'What this server is about …'
  tool_provider: App\Mcp\Tools   # implements Atwx\RestApi\Mcp\ToolProvider

Tools throw NotFoundException, ForbiddenException, InvalidArgumentException or ValidationException for failures the caller can fix; they are returned as tool results with isError: true, so the model can correct itself.

Connect Claude Code:

claude mcp add --transport http myapp https://<host>/mcp --header "Authorization: Bearer app_…"

Tests

From a project root with this module installed (DB user needs CREATE DATABASE):

vendor/bin/phpunit vendor/atwx/silverstripe-restapi/tests