Cognition
~/cognition ❯ call-cognition-from-your-own-code
Technical4 min read

Call Cognition from your own code

Use Cognition as an API: one HTTPS endpoint, your Cognition key, and the same tools your coding agents call. Works from curl, Python, TypeScript or any MCP client.

This guide uses the same MCP endpoint as your coding agent. It speaks JSON-RPC over HTTPS: send a tool name and arguments to call a tool available to your key from a script, backend job or app.

Note

Create a Cognition install key at lamlab.ai/start. A personal key (cog_me_*) can belong to a solo or team workspace. In a team workspace, it can read memory and skills shared with you and can save if it has write permission. Keep the key on your server, in an environment variable, never in browser code.

The endpoint

POST https://www.cognitionus.com/api/integrations/claude-code/mcp/v1
Authorization: Bearer $COGNITION_API_KEY
Content-Type: application/json
Accept: application/json, text/event-stream
Both Accept types are required by the MCP transport. A missing or invalid key returns 401.

Try it with curl

curl -sS https://www.cognitionus.com/api/integrations/claude-code/mcp/v1 \
  -H "Authorization: Bearer $COGNITION_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call",
       "params":{"name":"cognition_list_workspaces","arguments":{}}}'
The answer is in result.content[0].text, which is itself a JSON string. Parse it a second time.

Python, no extra packages

import json, os, urllib.request

URL = "https://www.cognitionus.com/api/integrations/claude-code/mcp/v1"

def call(tool, args):
    body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
                       "params": {"name": tool, "arguments": args}}).encode()
    req = urllib.request.Request(URL, data=body, headers={
        "Authorization": f"Bearer {os.environ['COGNITION_API_KEY']}",
        "Content-Type": "application/json",
        "Accept": "application/json, text/event-stream",
    })
    with urllib.request.urlopen(req) as res:
        return json.loads(json.load(res)["result"]["content"][0]["text"])

print(call("cognition_recall", {"query": "how we handle deploys"}))

TypeScript, with the MCP SDK

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const client = new Client({ name: "my-app", version: "1.0.0" });
await client.connect(new StreamableHTTPClientTransport(
  new URL("https://www.cognitionus.com/api/integrations/claude-code/mcp/v1"),
  { requestInit: { headers: { Authorization: `Bearer ${process.env.COGNITION_API_KEY}` } } },
));

try {
  const res = await client.callTool({ name: "cognition_recall", arguments: { query: "how we handle deploys" } });
  const block = Array.isArray(res.content) ? res.content[0] : undefined;
  if (res.isError || block?.type !== "text") throw new Error("Recall did not return a text result");
  console.log(JSON.parse(block.text));
} finally {
  await client.close();
}
npm install @modelcontextprotocol/sdk. client.listTools() lists tools available to this key and their input schemas.

The five calls most apps need

ToolArgumentsWhat it returns
cognition_assistintent_text, optional project_keyThe skills, decisions and project context that apply to a task
cognition_recallquery, optional project_keyWhat Cognition knows about a topic
cognition_recent_sessionsoptional project_key, limitWhere work left off: the latest handoffs
cognition_list_workspacesnoneYour workspaces and the ones shared with you
cognition_remembertext, optional project_keySaves a memory. This writes: only send what the person approved

Every other tool works the same way. Call tools/list (or client.listTools()) to see each one with its input schema.

Good to know

  • Calls use the key's identity, workspace and permissions. Private workspaces stay private.
  • For a save, use a key with write permission and check the tool's approval requirements.
  • Check for JSON-RPC errors and MCP tool errors, even on a successful HTTP response. The examples parse JSON text for their specific tools; other tools can return plain text or use different fields, so do not assume every result has an ok field.
  • For aggregate usage rather than memory tools, use the separate metrics API described at lamlab.ai/docs/metrics-api.
  • Never put secrets, credentials or raw personal data into remember or save calls.