Anthropic API Integration
Use the Json2doc MCP server with Anthropic's API to generate documents programmatically.
Prerequisites
- Anthropic API Key from console.anthropic.com
- Json2doc API Key from app.json2doc.com
- Python 3.7+ with
anthropicpackage
Installation
pip install anthropic
Basic Usage
import anthropic
client = anthropic.Anthropic(api_key="your-anthropic-api-key")
response = client.beta.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=[
{
"role": "user",
"content": "Create a PDF document with a title 'Project Report' and a paragraph about our Q4 results."
}
],
mcp_servers=[
{
"type": "url",
"url": "https://mcp.json2doc.com/mcp",
"name": "json2doc",
"authorization_token": "your-json2doc-api-key"
}
],
extra_headers={
"anthropic-beta": "mcp-client-2025-04-04"
}
)
print(response.content)
warning
Replace your-anthropic-api-key and your-json2doc-api-key with your actual API keys.
Configuration Options
{
"type": "url", # Required: Must be "url"
"url": "https://mcp.json2doc.com/mcp", # MCP server endpoint
"name": "json2doc", # Name for the MCP server
"authorization_token": "your-json2doc-api-key" # Your Json2doc API key
}
Important:
- The
extra_headerswithanthropic-beta: mcp-client-2025-04-04is required for MCP support - Use
client.beta.messages.create()instead of the regularmessages.create() - The authorization token is your Json2doc API key
Examples
Example 1: Generate Invoice
import anthropic
client = anthropic.Anthropic(api_key="your-anthropic-api-key")
invoice_data = {
"customer": "Acme Corp",
"amount": 2500.00,
"items": [
{"description": "Consulting Services", "amount": 2000.00},
{"description": "Support Package", "amount": 500.00}
]
}
response = client.beta.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=[
{
"role": "user",
"content": f"Create a professional invoice PDF for {invoice_data['customer']} "
f"with total amount ${invoice_data['amount']}. "
f"Include these items: {invoice_data['items']}"
}
],
mcp_servers=[
{
"type": "url",
"url": "https://mcp.json2doc.com/mcp",
"name": "json2doc",
"authorization_token": "your-json2doc-api-key"
}
],
extra_headers={
"anthropic-beta": "mcp-client-2025-04-04"
}
)
print(response.content)
Example 2: Batch Document Generation
import anthropic
client = anthropic.Anthropic(api_key="your-anthropic-api-key")
customers = ["Acme Corp", "TechStart Inc", "Global Solutions"]
for customer in customers:
response = client.beta.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=[
{
"role": "user",
"content": f"Generate a monthly status report PDF for {customer}. "
f"Include sections for: Overview, Key Metrics, Achievements, Next Steps."
}
],
mcp_servers=[
{
"type": "url",
"url": "https://mcp.json2doc.com/mcp",
"name": "json2doc",
"authorization_token": "your-json2doc-api-key"
}
],
extra_headers={
"anthropic-beta": "mcp-client-2025-04-04"
}
)
print(f"Generated report for {customer}: {response.content}")
Troubleshooting
Authentication Errors
Solutions:
- Verify your Json2doc API key in the dashboard
- Check the authorization token is set correctly
- Ensure no extra spaces in the key
- Verify the key hasn't expired
Beta API Errors
Solutions:
- Ensure you're using
client.beta.messages.create() - Verify the
extra_headersincludesanthropic-beta: mcp-client-2025-04-04 - Check you have access to the beta API features
Connection Issues
Solutions:
- Verify the URL:
https://mcp.json2doc.com/mcp - Check your internet connection
- Ensure firewall allows the connection
Rate Limiting
Solutions:
- Check your Json2doc plan limits in the dashboard
- Implement exponential backoff in your code
- Monitor your usage