Follow these steps to start using Model Context Protocol in your AI applications.
Install the MCP package using your favorite package manager. MCP works with Node.js, Python, and other environments.
npm install @modl/mcp
yarn add @modl/mcp
Define your context schema and create your first MCP context instance. This example shows a simple assistant with basic memory and tools.
import { MCPContext } from '@modl/mcp'; // Create a simple assistant context const assistant = new MCPContext({ systemInstruction: "You are a helpful assistant.", userGoal: "Answer questions about programming", memory: { shortTerm: [], longTerm: { userPreferences: { codeExamples: true, detailedExplanations: true } } }, tools: [ { name: "searchDocumentation", description: "Search programming documentation", parameters: { query: "string", language: "string" } } ] }); // Export the context export default assistant;
MCP works with any LLM provider. Here's an example using OpenAI, but you can use Anthropic, Google, or any other provider.
import OpenAI from 'openai'; import assistant from './index.js'; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); async function main() { // Compile the context for OpenAI const context = assistant.compileFor('openai'); // Use the context in your OpenAI call const response = await openai.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: context.systemMessage }, { role: 'user', content: 'How do I use async/await in JavaScript?' } ], tools: context.tools }); console.log(response.choices[0].message.content); } main();
As your application runs, you can update the context and memory to maintain state and improve responses.
import assistant from './index.js'; // Update user goal assistant.updateContext({ userGoal: "Debug a React component" }); // Add to short-term memory assistant.updateMemory({ shortTerm: [ ...assistant.memory.shortTerm, { type: "interaction", query: "How do I use async/await in JavaScript?", response: "Async/await is a way to handle promises in JavaScript..." } ] }); // Update long-term memory assistant.updateMemory({ longTerm: { ...assistant.memory.longTerm, topics: ["JavaScript", "Async Programming"] } });
Now that you've created your first MCP context, explore these resources to learn more:
Learn about the fundamental concepts behind MCP
See MCP in action with real-world use cases
Detailed documentation of all MCP methods and options