This project demonstrates how to integrate LangChain with the Xero MCP (Model Context Protocol) Server to create AI agents that can manage Xero accounting operations using natural language.
This TypeScript implementation showcases advanced LangChain patterns with the Xero MCP server, including specialized agents, complex workflows, and various integration patterns using the @langchain/mcp-adapters library.
- 🤖 Specialized Agent Demos: Different agents for invoicing, contacts, and general operations
- 📝 Invoice Management: Create, find, and analyze invoices with AI assistance
- 👥 Contact Operations: Manage customers and suppliers using natural language
- 🔄 Multi-Step Workflows: Complex business process automation
- 📊 Organization Insights: Get AI-powered analysis of your Xero data
- 💬 Interactive Mode: Real-time chat with your Xero AI assistant
- 🔧 LangGraph Integration: Uses the latest LangGraph prebuilt agents
- Node.js (version 18 or higher)
- npm or yarn
- OpenAI API Key: Get from OpenAI Platform
- Xero Custom Connection Credentials:
- Xero Client ID
- Xero Client Secret
- Get these by creating a custom connection Xero app at Xero Developer Portal
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in this directory with:OPENAI_API_KEY=your_openai_api_key_here XERO_CLIENT_ID=your_xero_client_id_here XERO_CLIENT_SECRET=your_xero_client_secret_here
Important: You must have valid Xero API credentials and an active OpenAI API key for the application to work properly.
To start the application:
npm start
# or
npx ts-node index.tsThe application includes six interactive demo modes:
- Invoice Specialist Agent - Focused on invoice creation and management
- Contact Manager Agent - Customer and supplier management
- Multi-Step Workflow - Monthly invoicing process automation
- Invoice Analysis - AI-powered invoice insights and recommendations
- Interactive Xero Agent - Real-time chat interface
- Organization Insights - Comprehensive Xero account analysis
- Find and manage draft invoices
- Create invoices for customers
- Calculate billing amounts and line items
- Track invoice statuses and workflows
- Create new customers and suppliers
- Search and find existing contacts
- Manage contact information and details
- Validate contact data for accuracy
- Execute complex business processes
- Coordinate multiple Xero operations
- Provide step-by-step guidance
- Automate routine monthly tasks
- Analyze Xero organization data
- Provide business recommendations
- Generate reports and summaries
- Track key performance indicators
// Natural language examples the agent can understand:
"Create a new customer called 'Demo Corp' with email [email protected]"
"Find all active customers"
"Create an invoice for Demo Corp for 10 hours of consulting at $150 per hour""Find all draft invoices"
"Show me recent invoices that are overdue"
"Analyze invoice payment patterns""Get organization details and provide insights"
"Summarize my contact database"
"Analyze recent invoice trends"- Uses
@langchain/mcp-adaptersfor seamless MCP integration - Leverages
createReactAgentfrom LangGraph for intelligent decision-making - Implements GPT-4o for enhanced reasoning capabilities
- Automatic tool discovery and wrapping
// Agent creation with MCP integration
const client = new MultiServerMCPClient({
xero: {
transport: 'stdio',
command: 'npx',
args: ['-y', '@xeroapi/xero-mcp-server@latest'],
env: { XERO_CLIENT_ID, XERO_CLIENT_SECRET }
}
});
const tools = await client.getTools();
const agent = createReactAgent({ llm, tools });- Xero uses UUID format for contact IDs (e.g.,
12345678-1234-1234-1234-123456789abc) - Always find customers first to get valid contact IDs before creating invoices
- The agents will automatically handle contact ID management
- Start with contact creation if you need new customers/suppliers
- Use natural language - the agents understand conversational requests
- Review agent responses for confirmation of successful operations
- Leverage multi-step workflows for complex business processes
The application includes comprehensive error handling for:
- Missing environment variables
- Invalid API credentials
- Network connectivity issues
- Xero API rate limits
- Invalid user inputs
-
Environment variable errors:
- Ensure
.envfile exists with all required variables - Check that API keys are valid and active
- Ensure
-
Xero authentication issues:
- Verify Xero app credentials are correct
- Ensure your Xero app has proper permissions
-
Node.js version issues:
- Use Node.js 18 or higher
- Update npm to the latest version
-
MCP Server connection issues:
- Ensure npx is available (comes with Node.js)
- Check internet connectivity for downloading the MCP server
# Start with development mode
npm start
# Build TypeScript
npm run build
# Type checking
npx tsc --noEmittypescript/langchain/
├── index.ts # Main application file
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
| Variable | Description | Required |
|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | Yes |
XERO_CLIENT_ID |
Xero app client ID | Yes |
XERO_CLIENT_SECRET |
Xero app client secret | Yes |
| Package | Version | Purpose |
|---|---|---|
@langchain/core |
^0.3.67 | Core LangChain functionality |
@langchain/langgraph |
^0.4.3 | Agent creation and workflows |
@langchain/openai |
^0.6.4 | OpenAI model integration |
@langchain/mcp-adapters |
^0.6.0 | MCP protocol adapters |
dotenv |
^16.4.5 | Environment variable management |
To extend this integration:
- Add new specialized agents in the main file
- Implement additional demo scenarios with new functions
- Create custom workflows for specific business processes
- Add error handling for specific Xero API scenarios
This project is part of the Xero Agent Toolkit. Please refer to the main project license.
For issues related to:
- LangChain: Check LangChain documentation
- OpenAI API: Visit OpenAI documentation
- Xero API: Visit Xero Developer Portal
- This Integration: Create an issue in the main repository