A sophisticated TypeScript/Node.js implementation of an Azure OpenAI MCP (Model Context Protocol) Agent that provides intelligent assistance for Azure Functions development using modern TypeScript patterns.
- π€ Azure OpenAI Integration - Powered by OpenAI SDK v4.67.3 with Azure support
- π§ Dynamic MCP Tool Discovery - Tools are discovered dynamically from MCP server (NO hardcoding)
- π‘οΈ Enterprise Security - Azure Identity integration with managed identity support
- π Interactive CLI - Rich console interface with command support
- ποΈ TypeScript - Full type safety with strict TypeScript configuration
- π Modern ESM - ES modules with async/await patterns
- βοΈ Environment Configuration - Secure credential management with dotenv
- π Streaming Support - Real-time response streaming for better user experience
- πΎ Conversation Persistence - Optional Cosmos DB integration for conversation history
- π Session Management - Unique session IDs for conversation tracking
- π― Proper MCP Protocol - Full compliance with MCP protocol using tools/list and tools/call
azure-openai-mcp-agent/
βββ src/
β βββ index.ts # Main entry point with configuration
β βββ agent/
β β βββ loop.ts # Core agent orchestration
β βββ tools/
β β βββ mcp-server.ts # MCP tool implementations
β βββ client/
β β βββ cli.ts # Interactive CLI interface
β βββ types/
β βββ index.ts # TypeScript type definitions
βββ package.json # Node.js dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ .env # Environment variables (create from .env.example)
- Node.js 18+
- npm or yarn
- Azure OpenAI service instance
- Azure subscription (for production deployment)
-
Clone and install dependencies:
npm install
-
Configure environment variables:
# Copy the example environment file and edit with your credentials cp .env.example .env # Edit .env with your Azure OpenAI credentials
-
Required environment variables:
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/ AZURE_OPENAI_API_KEY=your-api-key AZURE_OPENAI_DEPLOYMENT_NAME=your-deployment-name MCP_SERVER_PORT=3000 MCP_SERVER_URL=http://localhost:3000/mcp
-
Optional Cosmos DB configuration (for conversation persistence):
Identity-based authentication (recommended for production):
COSMOS_ENDPOINT=https://your-cosmos-account.documents.azure.com:443/ COSMOS_DATABASE_ID=agent-conversations COSMOS_CONTAINER_ID=sessions COSMOS_USE_IDENTITY=true
Key-based authentication (legacy, still supported):
COSMOS_ENDPOINT=https://your-cosmos-account.documents.azure.com:443/ COSMOS_KEY=your-cosmos-key COSMOS_DATABASE_ID=agent-conversations COSMOS_CONTAINER_ID=sessions
π Recommended: For a complete, automated Cosmos DB setup (including account creation, RBAC, and testing), use the cosmosdb-identity-rbac-setup GitHub prompt. This guide walks you through the entire process with reusable scripts and environment variables.
For legacy/manual details, see COSMOS_IDENTITY.md
# Terminal 1: Start MCP server
npm run mcp-server
# Terminal 2: Start CLI client
npm run dev
# or for watch mode
npm run dev:watchnpm run build
npm startnpm run demoOnce the CLI starts, you can use these commands:
/help- Show available commands/clear- Clear conversation history/history- Show conversation history/tools- Show registered tools/streaming- Toggle streaming mode on/off/session- Show session information (ID, message count, etc.)/exit- Exit the application
- "How do I create a timer trigger in Azure Functions?"
- "Show me a sample for timer trigger"
- "What are the best practices for Azure Functions performance?"
- "Show me how to implement Durable Functions"
- "How do I use Azure Functions with Cosmos DB?"
Run protective tests to ensure dynamic tool discovery is working correctly:
# Quick regression check (30 seconds)
npm run test:regression
# Full integration test (2 minutes)
npm run test:mcp-integrationThese tests verify:
- β No hardcoded tool names in switch statements
- β Proper MCP protocol calls (tools/list, tools/call)
- β Correct SSE Accept headers
- β Dynamic tool registration pattern
- β TypeScript compilation success
β οΈ Important: Runnpm run test:regressionbefore making any changes to ensure you don't break the dynamic discovery system!
# Terminal 1: Start MCP server
npm run mcp-server
# Terminal 2: Test CLI discovery
npm run devThe MCP server exposes a dynamic tool called find-azfunc-samples that provides intelligent access to the Awesome AZD Gallery.
- Real-time Search: Searches the live Awesome AZD Gallery for Azure Functions samples
- Smart Filtering: Filters by query terms, author, or specific Azure services
- Live Data: Fetches fresh sample data from the official Microsoft gallery JSON endpoint
- Azure Functions Focus: Pre-filtered to show only samples that include Azure Functions
- Dynamic Fetching: Retrieves latest samples from
https://raw.githubusercontent.com/Azure/awesome-azd/main/website/static/templates.json - Intelligent Filtering: Applies user queries against sample titles, descriptions, authors, and Azure services
- Structured Results: Returns formatted sample information with titles, descriptions, GitHub links, and Azure service tags
# Terminal 1: Start MCP server
npm run mcp-server
# Terminal 2: Start CLI and try these queries
npm run dev
# Example queries:
"Show me python azure functions samples"
"Find samples by author Microsoft"
"What Azure Functions samples use Cosmos DB?"
"Show me timer trigger examples"queryorquestion: Search terms for title/description matchingauthor: Filter by sample author nameazureService: Filter by specific Azure service (e.g., "cosmosdb", "sql", "storage")
- Real-time response streaming for better user experience
- Toggle between streaming and non-streaming modes with
/streamingcommand - Simulated word-by-word response delivery
- Optional Azure Cosmos DB integration for conversation storage
- Automatic session management with unique session IDs
- Messages include timestamps and session tracking
- Load previous conversations when restarting with the same session
- Unique UUID-based session identifiers
- Session information display with
/sessioncommand - Conversation history tied to specific sessions
- Cross-session conversation persistence
- Authentication: Uses Azure Identity SDK with support for Managed Identity in production, API keys for development
- Cosmos DB Identity: Supports keyless authentication using Azure AD identities (recommended for production)
- Credentials: Environment-based configuration, never hardcoded values
- Error Handling: Comprehensive error handling with detailed logging
- Type Safety: Full TypeScript implementation with strict type checking enabled
- Modern Security: ES modules with secure coding patterns
npm run lintnpm testnpm run build- Authentication Issues: Ensure your Azure OpenAI credentials are correct in your
.envfile - Connection Problems: Check your Azure OpenAI endpoint URL and deployment name
- Module Errors: Run
npm installto ensure all dependencies are installed - TypeScript Errors: Run
npm run buildto check for compilation issues - Permission Issues: Ensure your Azure OpenAI resource has the correct permissions configured
- Follow TypeScript best practices
- Add comprehensive error handling
- Include unit tests for new features
- Update documentation for changes
MIT License - see LICENSE file for details