Integración Rápida
Comienza con servidores MCP en minutos con nuestros ejemplos paso a paso.
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
// Initialize MCP client
const transport = new StdioClientTransport({
command: 'node',
args: ['path/to/server.js']
});
const client = new Client(
{ name: "my-ai-agent", version: "1.0.0" },
{ capabilities: {} }
);
// Connect to MCP server
await client.connect(transport);
// List available tools
const { tools } = await client.listTools();
console.log('Available tools:', tools);
// Call a tool
const result = await client.callTool({
name: 'read_file',
arguments: { path: '/path/to/file.txt' }
});
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
// Create MCP server
const server = new Server(
{ name: "file-manager", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
// Define tools
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "read_file",
description: "Read contents of a file",
inputSchema: {
type: "object",
properties: {
path: { type: "string", description: "File path" }
},
required: ["path"]
}
}
]
}));
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (name === "read_file") {
const content = await fs.readFile(args.path, 'utf-8');
return { content: [{ type: "text", text: content }] };
}
});
// List available resources
const { resources } = await client.listResources();
// Read a specific resource
const resource = await client.readResource({
uri: "file:///path/to/document.txt"
});
// Subscribe to resource changes
await client.subscribeToResource({
uri: "file:///path/to/watched-file.txt"
});
// Handle resource updates
client.onNotification(ResourceUpdatedNotificationSchema, (notification) => {
console.log('Resource updated:', notification.params.uri);
});
// Provide dynamic resources in server
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
resources: [
{
uri: "file:///docs/readme.md",
name: "Project README",
description: "Main project documentation",
mimeType: "text/markdown"
}
]
}));
// List available prompts
const { prompts } = await client.listPrompts();
// Get a specific prompt
const prompt = await client.getPrompt({
name: "code_review",
arguments: {
language: "javascript",
file_path: "/src/components/App.js"
}
});
// Define prompts in server
server.setRequestHandler(ListPromptsRequestSchema, async () => ({
prompts: [
{
name: "code_review",
description: "Perform code review analysis",
arguments: [
{
name: "language",
description: "Programming language",
required: true
},
{
name: "file_path",
description: "Path to file for review",
required: true
}
]
}
]
}));
// Handle prompt requests
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (name === "code_review") {
return {
description: `Review ${args.language} code`,
messages: [
{
role: "user",
content: {
type: "text",
text: `Please review this ${args.language} file: ${args.file_path}`
}
}
]
};
}
});
¿Listo para Mejorar tu IA?
Descubre servidores MCP, guías de integración y mejores prácticas en nuestro blog completo.