MCP
Create a fluent MCP (Model Context Protocol) client for consuming external MCP servers. MCP enables AI applications to connect to tools, data sources, and external systems through a standardized protocol.
🔌 MCP Client-Server Flow
Syntax
MCP(baseURL)Parameters
baseURL
string
Yes
The base URL of the MCP server to connect to
Returns
Returns an MCPClient instance with fluent API for:
Configuration:
withTimeout(),withBearerToken(),withHeaders()Callbacks:
onSuccess(),onError()Discovery:
listTools(),listResources(),listPrompts()Invocation:
send(),callTool(),getResource(),getPrompt()
Examples
Basic MCP Client
With Configuration
Callback Handlers
Discover Tools
Call Specific Tool
Discover Resources
Get Resource
Discover Prompts
Get Prompt
Authentication
Custom Headers
Error Handling
Timeout Configuration
Integration with AI Agent
Multiple MCP Servers
Health Check
Logging Wrapper
Retry Logic
Dynamic Tool Discovery
Caching MCP Responses
Notes
🔌 Protocol: Implements Model Context Protocol standard for AI integrations
🌐 HTTP-Based: Works with any MCP server over HTTP/HTTPS
🔧 Fluent API: Chainable configuration methods
🎯 Discovery: Dynamically discover tools, resources, and prompts
🔐 Security: Supports bearer tokens and custom headers
⚡ Configurable: Timeout and header customization
📡 Callbacks: Success and error handlers for async workflows
Related Functions
MCPServer()- Create MCP server to expose toolsaiTool()- Create AI tools (can wrap MCP tools)aiAgent()- Use MCP tools with agents
Best Practices
✅ Discover before using - List tools/resources to understand capabilities
✅ Handle errors - Check response status and handle errors gracefully
✅ Use timeouts - Set appropriate timeouts for network operations
✅ Secure connections - Use HTTPS and bearer tokens for production
✅ Cache responses - Cache expensive MCP calls when appropriate
✅ Retry on failure - Implement retry logic for transient errors
❌ Don't hardcode URLs - Use environment variables for MCP server URLs
❌ Don't ignore errors - Always check if response is error before using data
❌ Don't skip authentication - Use tokens for protected MCP servers
Last updated