Integrating External Tools
Connect your AI agents to databases, APIs, and other services
Integrating External Tools
Learn how to connect your MCP Server agents to external services, databases, and APIs.
Overview
MCP Server supports integration with:
Database Integration
PostgreSQL
import { MCPClient } from '@mcpserver/sdk';
const client = new MCPClient({
apiKey: process.env.MCP_API_KEY,
});
// Configure database connection
await client.databases.connect({
type: 'postgresql',
connection: {
host: process.env.DB_HOST,
port: 5432,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
},
});
// Query the database
const results = await client.databases.query({
sql: 'SELECT * FROM customers WHERE active = true',
});
MongoDB
await client.databases.connect({
type: 'mongodb',
connection: {
uri: process.env.MONGODB_URI,
},
});
const documents = await client.databases.find({
collection: 'users',
filter: { status: 'active' },
});
API Integration
REST APIs
await client.apis.register({
name: 'stripe',
baseUrl: 'https://api.stripe.com',
auth: {
type: 'bearer',
token: process.env.STRIPE_API_KEY,
},
endpoints: [
{
name: 'list-customers',
method: 'GET',
path: '/v1/customers',
},
{
name: 'create-payment-intent',
method: 'POST',
path: '/v1/payment_intents',
},
],
});
// Use the API
const customers = await client.apis.call('stripe', 'list-customers');
GraphQL
await client.apis.register({
name: 'github',
type: 'graphql',
endpoint: 'https://api.github.com/graphql',
auth: {
type: 'bearer',
token: process.env.GITHUB_TOKEN,
},
});
const result = await client.apis.query('github',
query {
viewer {
repositories(first: 10) {
nodes {
name
description
}
}
}
}
);
File System Access
// Read files
const content = await client.files.read('/path/to/file.json');
// Write files
await client.files.write('/path/to/output.txt', 'Hello, world!');
// List directory
const files = await client.files.list('/path/to/directory');
Cloud Storage
AWS S3
await client.storage.connect({
provider: 's3',
config: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
region: 'us-east-1',
},
});
// Upload file
await client.storage.upload({
bucket: 'my-bucket',
key: 'uploads/file.pdf',
body: fileBuffer,
});
// Download file
const file = await client.storage.download({
bucket: 'my-bucket',
key: 'uploads/file.pdf',
});
Security Best Practices
1. Never expose API keys in client-side code
2. Use environment variables for sensitive credentials
3. Implement rate limiting to prevent abuse
4. Validate all inputs before passing to external services
5. Use HTTPS for all API communications
Error Handling
try {
const result = await client.apis.call('external-service', 'endpoint');
} catch (error) {
if (error.code === 'RATE_LIMIT_EXCEEDED') {
// Wait and retry
await sleep(5000);
return retry();
} else if (error.code === 'UNAUTHORIZED') {
// Refresh credentials
await refreshAuth();
} else {
// Log and handle
console.error('API error:', error);
}
}