Getting Started with MCP

Follow these steps to start using Model Context Protocol in your AI applications.

1. Install the MCP package

Install the MCP package using your favorite package manager. MCP works with Node.js, Python, and other environments.

npm
npm install @modl/mcp
yarn
yarn add @modl/mcp

2. Create your first MCP context

Define your context schema and create your first MCP context instance. This example shows a simple assistant with basic memory and tools.

index.js
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;

3. Use with your LLM provider

MCP works with any LLM provider. Here's an example using OpenAI, but you can use Anthropic, Google, or any other provider.

openai-example.js
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();

4. Update context and memory

As your application runs, you can update the context and memory to maintain state and improve responses.

update-context.js
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"]
  }
});

Next Steps

Now that you've created your first MCP context, explore these resources to learn more: