Introducing @riktajs/mcp: Connect AI Assistants to Your Backend
We're thrilled to announce @riktajs/mcp - seamless integration of the Model Context Protocol (MCP) for Rikta applications. Now you can expose your backend services to AI assistants like Claude, GPT, and other LLM-powered tools with just a few decorators.
What is MCP?โ
The Model Context Protocol is an open standard that allows AI applications to securely connect to external data sources and tools. Think of it as a universal API that AI assistants can use to interact with your services.
With MCP, your backend becomes an intelligent assistant's toolkit - whether it's reading files, querying databases, or executing business logic.
Why @riktajs/mcp?โ
Building MCP servers from scratch can be complex. @riktajs/mcp brings Rikta's philosophy of simplicity to MCP integration:
- ๐ค Decorator-based API - Define tools with
@MCPTool,@MCPResource,@MCPPrompt - ๐ Auto-discovery - No manual registration needed
- ๐ Zod Integration - Type-safe schemas that convert automatically
- ๐ก Production Ready - SSE support, Redis scaling, full TypeScript
- โก Zero Config - Works out of the box
Quick Startโ
Install the package:
npm install @riktajs/mcp
Create a simple calculator service:
import { Injectable, z } from '@riktajs/core';
import { MCPTool } from '@riktajs/mcp';
@Injectable()
class CalculatorService {
@MCPTool({
name: 'add',
description: 'Add two numbers',
inputSchema: z.object({
a: z.number().describe('First number'),
b: z.number().describe('Second number'),
}),
})
async add(params: { a: number; b: number }) {
return {
content: [{
type: 'text',
text: `Result: ${params.a + params.b}`,
}],
};
}
}
Register the MCP server in your app:
import { Rikta } from '@riktajs/core';
import { registerMCPServer } from '@riktajs/mcp';
const app = await Rikta.create({ port: 3000 });
await registerMCPServer(app, {
serverInfo: { name: 'calculator', version: '1.0.0' },
instructions: 'A calculator service for basic math operations',
});
await app.listen();
// MCP server available at http://localhost:3000/mcp
That's it! Your backend is now accessible to AI assistants.
Real-World Use Casesโ
1. File System Assistantโ
Give AI the ability to read and list files:
@Injectable()
class FileSystemService {
@MCPTool({
name: 'list_files',
description: 'List files in a directory',
inputSchema: z.object({
path: z.string().describe('Directory path'),
showHidden: z.boolean().optional().default(false),
}),
})
async listFiles(params: { path: string; showHidden?: boolean }) {
const files = await fs.readdir(params.path);
const filtered = params.showHidden
? files
: files.filter(f => !f.startsWith('.'));
return {
content: [{
type: 'text',
text: `Files in ${params.path}:\n${filtered.join('\n')}`,
}],
};
}
@MCPResource({
uriPattern: 'file://read',
name: 'Read File',
description: 'Read file contents. Use ?path=<filepath>',
mimeType: 'text/plain',
})
async readFile(uri: string) {
const url = new URL(uri);
const path = url.searchParams.get('path');
const content = await fs.readFile(path, 'utf-8');
return {
contents: [{
uri,
text: content,
mimeType: 'text/plain',
}],
};
}
}
Now AI assistants can navigate your file system and read files!
2. Database Query Assistantโ
Let AI query your database:
@Injectable()
class DatabaseService {
@Autowired()
private userRepository!: UserRepository;
@MCPTool({
name: 'search_users',
description: 'Search users by name or email',
inputSchema: z.object({
query: z.string().describe('Search query'),
limit: z.number().min(1).max(100).default(10),
}),
})
async searchUsers(params: { query: string; limit: number }) {
const users = await this.userRepository.search(params.query, params.limit);
return {
content: [{
type: 'text',
text: `Found ${users.length} users:\n` +
users.map(u => `- ${u.name} <${u.email}>`).join('\n'),
}],
};
}
@MCPResource({
uriPattern: 'db://users',
name: 'Users Database',
description: 'Get all users. Use ?limit=N for pagination',
mimeType: 'application/json',
})
async getAllUsers(uri: string) {
const url = new URL(uri);
const limit = parseInt(url.searchParams.get('limit') || '50');
const users = await this.userRepository.find({ take: limit });
return {
contents: [{
uri,
text: JSON.stringify(users, null, 2),
mimeType: 'application/json',
}],
};
}
}
AI can now intelligently query and analyze your data!
3. Code Review Assistantโ
Create prompt templates for common tasks:
@Injectable()
class CodeReviewService {
@MCPPrompt({
name: 'code_review',
description: 'Generate a code review prompt',
arguments: [
{ name: 'language', description: 'Programming language', required: true },
{ name: 'code', description: 'Code to review', required: true },
{ name: 'focus', description: 'Specific focus (security, performance)' },
],
})
async codeReview(args: { language: string; code: string; focus?: string }) {
const focusArea = args.focus ? `\nFocus on: ${args.focus}` : '';
return {
messages: [{
role: 'user',
content: {
type: 'text',
text: `Review this ${args.language} code:${focusArea}\n\n\`\`\`${args.language}\n${args.code}\n\`\`\``,
},
}],
};
}
}
Testing Your MCP Serverโ
Use the official MCP Inspector for visual testing:
npx @modelcontextprotocol/inspector http://localhost:3000/mcp
Or test with curl:
# List available tools
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
}'
# Call a tool
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "add",
"arguments": { "a": 5, "b": 3 }
}
}'
Production Featuresโ
Horizontal Scaling with Redisโ
Deploy across multiple instances with Redis:
await registerMCPServer(app, {
serverInfo: { name: 'production-server', version: '1.0.0' },
redis: {
host: 'redis.example.com',
port: 6379,
password: process.env.REDIS_PASSWORD,
},
sessionTTL: 7200,
});
Real-time Updates with SSEโ
Enable Server-Sent Events for real-time communication:
await registerMCPServer(app, {
serverInfo: { name: 'realtime-server', version: '1.0.0' },
enableSSE: true,
heartbeat: true,
heartbeatInterval: 30000,
});
The Power of Zodโ
All input schemas use Zod, giving you:
- Type safety - Schemas are validated at runtime
- Auto-conversion - Zod schemas convert to JSON Schema for MCP
- Rich validation - Email, UUID, min/max, custom validators
const schema = z.object({
email: z.string().email().describe('User email address'),
age: z.number().min(18).max(120).describe('User age'),
role: z.enum(['admin', 'user', 'guest']).default('user'),
tags: z.array(z.string()).optional(),
});
Three Ways to Expose Functionalityโ
1. Tools - Functions AI Can Callโ
@MCPTool({
name: 'send_email',
description: 'Send an email',
inputSchema: z.object({
to: z.string().email(),
subject: z.string(),
body: z.string(),
}),
})
async sendEmail(params) { /* ... */ }
2. Resources - Data AI Can Readโ
@MCPResource({
uriPattern: 'config://app',
name: 'App Configuration',
description: 'Current app configuration',
mimeType: 'application/json',
})
async getConfig(uri: string) { /* ... */ }
3. Prompts - Template Conversationsโ
@MCPPrompt({
name: 'debug_help',
description: 'Get debugging assistance',
arguments: [
{ name: 'error', required: true },
],
})
async debugHelp(args) { /* ... */ }
Architectureโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AI Assistant (Claude, GPT) โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Protocol (JSON-RPC)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ @riktajs/mcp โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Auto-discovery Registry โ โ
โ โ @MCPTool @MCPResource โ โ
โ โ @MCPPrompt โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ @platformatic/mcp (Core) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโ โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Rikta Services โ
โ (Database, APIs, Business Logic) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Getting Startedโ
Install the package:
npm install @riktajs/mcp zod
Check out the documentation for complete guides and examples.
What's Next?โ
We're excited to see what you'll build with @riktajs/mcp! Here are some ideas:
- ๐๏ธ Database Assistant - Let AI query and analyze your data
- ๐ File Manager - AI-powered file operations
- ๐ Code Analyzer - Automated code reviews and analysis
- ๐ Analytics Dashboard - Natural language queries for metrics
- ๐ ๏ธ DevOps Helper - Manage deployments and infrastructure
Communityโ
Join our growing community:
We can't wait to see what you build! Share your MCP servers with us on Twitter using #RiktaMCP.
Happy coding! ๐