Skip to main content

OpenAI SDK (Agents)

Overview​

Integrate Civic with the OpenAI Agents SDK using hostedMcpTool() — the simplest way to use Civic with OpenAI. No manual tool looping or schema conversion required.

note

For the older manual function calling approach, see OpenAI SDK (Node.js).

Prerequisites​

Installation​

npm install @openai/agents

The openai package is bundled as a dependency — no need to install it separately.

Authentication​

For autonomous agents and server-side scripts — use a Civic token directly.

Generate a Civic Token​

  1. Log in to app.civic.com
  2. Click your account name in the bottom left
  3. Go to Install → MCP URL
  4. Click Generate Token and copy it immediately — it won't be shown again
warning

Never commit your token to source control. Store it in environment variables or a secrets manager. Tokens expire after 30 days.

Set Environment Variables​

CIVIC_TOKEN=your-civic-token-here
CIVIC_URL=https://app.civic.com/hub/mcp

For production agents, lock to a specific toolkit by appending a profile parameter:

CIVIC_URL=https://app.civic.com/hub/mcp?profile=your-toolkit-alias

Use the Token​

Pass the token as a Bearer token in the Authorization header:

headers = {"Authorization": f"Bearer {os.environ['CIVIC_TOKEN']}"}
headers: { Authorization: `Bearer ${process.env.CIVIC_TOKEN}` }
Full credentials guide

Token generation, URL parameters, OAuth vs token comparison

# .env
OPENAI_API_KEY=your_openai_api_key
CIVIC_TOKEN=your-civic-token-here
CIVIC_URL=https://app.civic.com/hub/mcp

Basic Setup​

import { Agent, hostedMcpTool, run } from '@openai/agents';

const agent = new Agent({
name: 'Civic Assistant',
model: 'gpt-4o-mini',
instructions: 'You are a helpful assistant with access to external tools through Civic.',
tools: [
hostedMcpTool({
serverLabel: 'civic',
serverUrl: process.env.CIVIC_URL!,
authorization: process.env.CIVIC_TOKEN!, // plain token string
}),
],
});

async function main() {
const result = await run(agent, 'List my GitHub repositories');
console.log(result.finalOutput); // the agent's final text response
}

main().catch(console.error);

The Agent automatically discovers available Civic tools, selects the right one, calls it, and returns the result.

Streaming​

const result = await run(agent, 'Your question here?', { stream: true });

for await (const event of result) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}

console.log(`Final result: ${result.finalOutput}`);

Tool Approval​

Control which tools require human approval before execution:

hostedMcpTool({
serverLabel: 'civic',
serverUrl: process.env.CIVIC_URL!,
authorization: process.env.CIVIC_TOKEN!,
requireApproval: {
never: { toolNames: ['github__list_repos', 'slack__search_messages'] },
always: { toolNames: ['github__delete_repo'] },
}
})

Strict Mode​

warning

Not all Civic tool schemas are compatible with OpenAI's strict: true structured outputs. Set strict to false on the Responses API when using Civic tools.

Comparison​

FeatureOpenAI Agents SDK (hostedMcpTool)OpenAI SDK (function calling)Vercel AI SDK
Setup ComplexityLowMediumMedium
Tool LoopingNo (automatic)Yes (manual)Yes (manual)
Tool ConversionNoYesYes
Best ForAgents and scriptsFull controlNext.js apps
StreamingYesYesYes