Read-only DynamoDB MCP
by gongarher
README.md
# Read-only DynamoDB MCP: a small HTTP + auth demo
> **From “let me dig through DynamoDB JSON” to “ask an assistant for a record.”**
> A self-contained [Model Context Protocol](https://modelcontextprotocol.io/) example with synthetic data by default. No AWS account is needed to try it.
```mermaid
flowchart LR
A["Person asks<br/>‘Show me a sample record’"] --> B["MCP client<br/>e.g. OpenCode"]
B -- "JSON-RPC over Streamable HTTP<br/>Bearer access token" --> C["MCP server<br/>validates token + exposes read tools"]
C --> D{"Data source"}
D --> E["Synthetic records<br/>default"]
D --> F["Your DynamoDB test table<br/>opt-in only"]
E --> C
F --> C
C -- "Readable JSON" --> B
B --> A
```
### The problem it solves
DynamoDB returns typed attributes such as `{"S":"value"}`, and exploring a table usually means switching between a chat, the AWS console and CLI syntax. This MCP gives an assistant **four narrow, read-only tools** (`describe_table`, `get_item`, `query_partition`, `sample_items`) and returns plain JSON. A question becomes a specific tool call rather than arbitrary database access. Samples and limits keep the interaction small; read-only still permits disclosure, so access control matters.
This demo is a **sanitized, standalone teaching version** of a DynamoDB explorer built while working on [mioapp.vip](https://mioapp.vip/) ([Android app on Google Play](https://play.google.com/store/apps/details?id=vip.mioapp.app)). It includes **no application data, production table names, credentials, or internal entity model**. The fixture contains only invented records (`DOC#sample-01`, etc.).
## Run locally in five minutes
Requires Node.js 22+ and npm.
```bash
npm ci
```
```bash
cp .env.example .env
```
Generate a fresh secret, then paste it into `MCP_DEMO_SECRET=` in your private `.env` file:
```bash
openssl rand -hex 32
```
Terminal 1: start the server (listens only on `127.0.0.1:3001`):
```bash
npm start
```
Terminal 2: generate a short-lived JWT locally:
```bash
export MCP_TOKEN="$(npm run --silent token)"
```
No token → **401 Unauthorized**:
```bash
curl -i http://127.0.0.1:3001/mcp
```
Valid token → initialize the MCP session:
```bash
curl -sS http://127.0.0.1:3001/mcp \
-H "Authorization: Bearer $MCP_TOKEN" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"demo","version":"1"}}}'
```
Call a tool to read an **invented** record:
```bash
curl -sS http://127.0.0.1:3001/mcp \
-H "Authorization: Bearer $MCP_TOKEN" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_item","arguments":{"PK":"DOC#sample-01","SK":"META"}}}'
```
Try calling `delete_item` instead: the server returns a tool error; there is no write operation in the implementation. The fixture never accesses AWS.
## Connect an MCP client
For example, add a **remote** MCP server in your OpenCode configuration (on the same computer). Store `MCP_TOKEN` in the environment before starting OpenCode; never put the token in a committed configuration file.
```json
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"dynamodb-demo": {
"type": "remote",
"url": "http://127.0.0.1:3001/mcp",
"oauth": false,
"headers": { "Authorization": "Bearer {env:MCP_TOKEN}" }
}
}
}
```
Restart OpenCode after configuring it. Try asking **“What example records can you show me?”** Logs on the server show the verified subject, tool and outcome without printing tokens, inputs or returned records. Tokens generated by `npm run token` expire after 60 minutes.
## Use your own *test* DynamoDB table (optional)
The default fixture requires no cloud permissions. For AWS, change `.env` to `DATA_SOURCE=aws`, set `DYNAMODB_TABLE_NAME` explicitly and provide `AWS_REGION`. Run `AWS_PROFILE=your-read-only-profile npm start`. This example assumes a table with **string keys named `PK` and `SK`**. It has no arbitrary table-name parameter: a caller cannot switch tables via a tool argument. Limit the server's IAM principal to `dynamodb:DescribeTable`, `dynamodb:GetItem`, `dynamodb:Query` and `dynamodb:Scan` for that table only. Do not use real customer records in a public demonstration.
The server only imports read commands, and tools cap query results at 20 and samples at 10. **Code-level read-only is not an IAM restriction:** another process using broad AWS credentials could still write. A narrowly scoped IAM identity is the second boundary. Scan limits cap returned pages, not necessarily total table read capacity or all data exposure.
## What the auth demo does (and does not)
- `src/token.ts` signs a **local demonstration JWT** using HS256 and a secret in `.env`. The server verifies signature, `iss` (issuer), `aud` (this server), `exp` (expiry), presence of `sub` (subject) and `scope=records:read` **on every HTTP request**. Missing or invalid token → 401; missing scope → 403. JWT payloads are readable, not encrypted.
- The shared-secret script is **not an OAuth authorization server or a login flow**. For real OAuth, use an external identity provider: the optional `MCP_JWKS_URL` mode accepts RS256 JWTs from an HTTPS issuer, checks audience and scope, and exposes protected-resource metadata. A client needs its own registration and Authorization Code + PKCE flow. That provider integration is illustrative, not tested against a live tenant.
- Never forward an MCP access token to AWS. The DynamoDB SDK uses separate AWS credentials. HTTP is deliberately loopback-only; to demo across machines, use SSH port forwarding or a properly configured HTTPS deployment. The server is not an Internet-facing deployment template.
- An MCP token grants access to all four tools in this example. There is **no per-user or per-tool policy**. `sub` is an identity claim from the validated token; the fixture token does not prove a human logged in.
## Verify
```bash
npm run typecheck
```
```bash
npm test
```
The tests use synthetic data and no AWS account. This repo should be created as a **new repository without importing the original application repo's Git history**.
License: [MIT](LICENSE).