> ## Documentation Index
> Fetch the complete documentation index at: /developers/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect external tools

> Connect your REST or MCP service to read records and save reviewed notes.

Connect your EHR or another service through REST or MCP. Cortexa can read from
connected tools and submit writes for clinician approval.

## Setup

1. Register your endpoint and tool definitions with your application API key.
2. Arrange review of the connector with Cortexa.
3. Connect each clinician using a Bearer credential or OAuth.
4. Call tools with the clinician's Partner JWT.

## 1. Register the connector

`POST /connectors` — use the `Cortexa-Api-Key` header and this JSON body:

```json theme={null}
{
  "name": "Synthetic EHR",
  "description": "Look up patients and save reviewed notes.",
  "transport": "rest",
  "endpoint": "https://ehr.example/cortexa",
  "auth": "bearer",
  "profile": "tools",
  "tools": [
    {
      "name": "get_patient",
      "description": "Read a patient by external ID.",
      "write": false,
      "method": "GET",
      "path": "/patient",
      "input_schema": {
        "type": "object",
        "properties": {"patient_id": {"type": "string"}},
        "required": ["patient_id"],
        "additionalProperties": false
      }
    },
    {
      "name": "push_note",
      "description": "Save a reviewed note.",
      "write": true,
      "method": "POST",
      "path": "/note",
      "input_schema": {
        "type": "object",
        "properties": {"patient_id": {"type": "string"}, "text": {"type": "string"}},
        "required": ["patient_id", "text"],
        "additionalProperties": false
      }
    }
  ]
}
```

The response includes `id`, `manifest_hash`, and `approved: false`. Save the ID as
`CONNECTOR_ID` and arrange review before making calls.

| Operation | Endpoint |
| - | - |
| List your application's connectors | `GET /connectors` |
| Replace a connector's full definition | `PUT /connectors/{connector_id}` |

These operations use the API key. Any definition change requires another review
and a new clinician connection, including changes to descriptions.

### REST requirements

| Setting | Requirement |
| - | - |
| Endpoint | Public HTTPS; local and private network addresses are not accepted |
| Tools | Up to 100 unique names with valid JSON input schemas; only local `$ref` references |
| Requests | Endpoint + tool path; GET arguments become query parameters, other methods receive JSON |
| Authentication | `Authorization: Bearer <connected credential>` |
| Writes | Set `write: true` for side effects; required for PUT, PATCH, DELETE, and `push_note` |
| Response | JSON, at most 2 MB; an optional `output_schema` validates the returned data |
| Timeout | 45 seconds; redirects are not followed |
| Deduplication | Persist the `Idempotency-Key` sent with writes to avoid repeating the same operation |

### EHR profile

Use `profile: "ehr"` to require these six tools. Your manifest defines their
input and output schemas.

| Tool | Purpose |
| - | - |
| `info` | Describe the connected practice |
| `list_practitioners` | List available practitioners |
| `list_appointments` | Find scheduled encounters |
| `search_patients` | Find a patient |
| `get_patient` | Retrieve the selected patient |
| `push_note` | Save a reviewed note; always a write tool |

### MCP requirements

Use `transport: "mcp"` and a **Streamable HTTP** endpoint. Copy each tool's name,
description, schemas, and annotations from `tools/list`, and set `write` explicitly.
REST `method` and `path` fields are ignored. Stdio and legacy HTTP+SSE are unsupported.

Cortexa checks the live tool catalog before calls. A change returns
`409 CONNECTOR_SCHEMA_CHANGED`; update the manifest, arrange review, and reconnect.
MCP writes send the stable call ID in `_meta.idempotencyKey` for server-side deduplication.

## 2. Connect the clinician

Use the clinician's Partner JWT. Each connection belongs to that clinician and
workspace.

### Bearer credential

```bash theme={null}
curl --request PUT "$CORTEXA_API_BASE/connectors/$CONNECTOR_ID/connection" \
  --header "Authorization: Bearer $CORTEXA_JWT" \
  --header 'Content-Type: application/json' \
  --data '{"token":"synthetic-practice-token"}'
```

The response returns connection details without the credential. To disconnect,
call `DELETE /connectors/{connector_id}/connection`. Reconnecting invalidates
pending write approvals.

### OAuth

Set `auth: "oauth2"` in the connector manifest and add an `oauth` object:

```json theme={null}
{
  "authorization_url": "https://ehr.example/oauth/authorize",
  "token_url": "https://ehr.example/oauth/token",
  "client_id": "your_registered_public_client",
  "redirect_uri": "https://partner.example/cortexa-callback",
  "scopes": ["patient.read", "note.write"]
}
```

Use a pre-registered public OAuth client with authorization code and PKCE S256.
Confidential-client secrets and dynamic registration are unsupported.

1. Call `POST /connectors/{connector_id}/oauth/authorize`.
2. Open the returned `authorization_url` for the clinician.
3. Send the callback's `code` and `state` to
   `POST /connectors/{connector_id}/oauth/complete` using the same grant.

State is single-use and expires after ten minutes. Cortexa stores credentials
and refreshes access tokens. Reauthorize if refresh fails or no refresh token is available.

## 3. Call a tool

`GET /connectors/tools` lists the tools available to the current clinician.

```bash theme={null}
curl --request POST "$CORTEXA_API_BASE/connectors/$CONNECTOR_ID/calls" \
  --header "Authorization: Bearer $CORTEXA_JWT" \
  --header 'Idempotency-Key: synthetic-visit-001-write' \
  --header 'Content-Type: application/json' \
  --data '{"tool_name":"push_note","arguments":{"patient_id":"synthetic-001","text":"Clinician-reviewed synthetic note."}}'
```

| Call type | Result |
| - | - |
| Read | `status: "completed"`, with REST data in `result.data` or the MCP call result |
| Write | `status: "needs_approval"`, with `call_id` and the arguments to review |

The connected clinician approves the write in Cortexa's approval interface.
Arrange this interface during onboarding. A Partner JWT cannot approve a write
by sending an `approved` flag. Cortexa Agent can also propose connected-tool
writes through its approval UI.

## Check a call's status

`GET /connector-calls/{call_id}` returns one of:

| Status | Next step |
| - | - |
| `needs_approval` | Wait for clinician review |
| `rejected` | The clinician declined the write |
| `executing` | Wait; the operation is in progress or its final result is not yet known |
| `completed` | Use the returned result |
| `failed` | Inspect the failure before starting another operation |
| `unknown` | Check the destination system before retrying the write |

Retry a lost response with the same key and arguments. Changing the arguments or
connection under that key returns `409`. A stalled `executing` or `unknown` call
may have reached the EHR; reconcile it by call ID before submitting a new write.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.