Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 1 | # Mastra × Composio |
| 2 | |
| 3 | Runnable references for using Composio tools with [Mastra](https://mastra.ai) |
| 4 | agents. Every example uses the **unauthenticated `HACKERNEWS` toolkit**, so no |
| 5 | connected account is required — set two keys and run. |
| 6 | |
| 7 | These examples double as tests: they are typechecked and linted on every PR, and |
| 8 | executed nightly against the staging backend. |
| 9 | |
| 10 | ## Setup |
| 11 | |
| 12 | ```bash |
| 13 | pnpm install |
| 14 | cp .env.example .env # then fill in COMPOSIO_API_KEY and OPENAI_API_KEY |
| 15 | ``` |
| 16 | |
| 17 | - `COMPOSIO_API_KEY` — [Composio dashboard](https://app.composio.dev) |
| 18 | - `OPENAI_API_KEY` — the examples default to OpenAI (`gpt-5-mini`) |
| 19 | |
| 20 | ## Examples |
| 21 | |
| 22 | | File | What it shows | Run | |
| 23 | | -------------------- | ----------------------------------------------------------------------------------- | --------------------------------------------- | |
| 24 | | `src/index.ts` | Direct tools: fetch one Composio tool as a Mastra tool | `bun ts/examples/mastra/src/index.ts` | |
| 25 | | `src/tool-router.ts` | **Tool Router** (v1-canonical): `composio.sessions.create(...)` → `session.tools()` | `bun ts/examples/mastra/src/tool-router.ts` | |
| 26 | | `src/cloudflare.ts` | The same Tool Router agent path on the **Cloudflare Workers** runtime | `bun run cf:dev` (then `GET localhost:8787/`) | |
| 27 | |
| 28 | From this folder you can also use `pnpm start` (index), `pnpm tool-router`, and |
| 29 | `pnpm cf:dev`. |
| 30 | |
| 31 | ## Using Anthropic instead of OpenAI |
| 32 | |
| 33 | The model setup lives in `src/hackernews-agent/direct.ts` and |
| 34 | `src/hackernews-agent/tool-router.ts`. Replace the OpenAI provider in the agent |
| 35 | you want to run and add `ANTHROPIC_API_KEY` to its environment type: |
| 36 | |
| 37 | ```ts |
| 38 | import { createAnthropic } from '@ai-sdk/anthropic'; |
| 39 | // ... |
| 40 | const anthropic = createAnthropic({ |
| 41 | apiKey: requireKey(env, 'ANTHROPIC_API_KEY'), |
| 42 | }); |
| 43 | model: anthropic('claude-haiku-4-5'), |
| 44 | ``` |
| 45 | |
| 46 | Add `@ai-sdk/anthropic` to the dependencies and configure the Anthropic key in |
| 47 | the Node environment or Worker bindings, depending on the runtime. |
| 48 | |
| 49 | ## Runtime notes |
| 50 | |
| 51 | - **Node/Bun:** `src/index.ts` and `src/tool-router.ts` read keys from |
| 52 | `process.env` via `dotenv`. |
| 53 | - **Cloudflare Workers:** `src/cloudflare.ts` reads keys from the Worker `env` |
| 54 | binding (no `process.env`). Provide both keys with |
| 55 | `wrangler secret put COMPOSIO_API_KEY` and |
| 56 | `wrangler secret put OPENAI_API_KEY`. CI validates the Worker build with |
| 57 | `wrangler deploy --dry-run`. |
| 58 | |
| 59 | ## Support |
| 60 | |
| 61 | - [Documentation](https://docs.composio.dev) |
| 62 | - [GitHub Issues](https://github.com/ComposioHQ/composio/issues) |