Skip to main content

Getting Started with the Model Context Protocol (MCP)

An introduction to MCP and how to build your first MCP server to connect AI models to your systems.

S

SmartTechLabs

SmartTechLabs - Intelligent Solutions for IoT, Edge Computing & AI

5 min read
Getting Started with the Model Context Protocol (MCP)
MCP connects AI models to your business systems through a standardized protocol

The Model Context Protocol (MCP) is an open standard developed by Anthropic for connecting AI assistants to external data sources and tools. In this guide, we’ll explore what MCP is and how to get started building MCP servers.

Open Standard

MCP is an open protocol—you can build servers that work with Claude and potentially other AI systems that adopt the standard.

What is MCP?

MCP provides a standardized way for AI models to:

CapabilityDescriptionExample
DiscoverFind available tools and resourcesList database tables
QueryAccess external data sourcesRead file contents
ExecutePerform actions in external systemsSend notifications
MaintainKeep context across interactionsSession state

Think of it as an API standard specifically designed for AI-to-system communication.


Core Concepts


flowchart TB
    subgraph Host["MCP Host (Claude)"]
        A[AI Model]
    end

    subgraph Protocol["MCP Layer"]
        B[JSON-RPC Transport]
        C[Schema Validation]
    end

    subgraph Server["MCP Server"]
        D[Resources]
        E[Tools]
        F[Prompts]
    end

    subgraph External["External Systems"]
        G[Databases]
        H[APIs]
        I[Files]
    end

    A <--> B
    B <--> C
    C <--> D & E & F
    D & E & F <--> G & H & I

    style Host fill:#e0f2fe,stroke:#0284c7
    style Protocol fill:#fef3c7,stroke:#d97706
    style Server fill:#dcfce7,stroke:#16a34a
    style External fill:#fce7f3,stroke:#db2777

    

MCP Components

ComponentPurposeExamples
ResourcesData sources AI can queryDatabase tables, file contents, API responses
ToolsActions AI can executeCreate records, send notifications, trigger workflows
PromptsPre-defined templatesStructured interaction patterns

Building Your First MCP Server

Let’s build a simple MCP server in TypeScript that provides access to a list of tasks.

Project Setup

1
2
3
4
5
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node

Basic Server Structure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// src/index.ts
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
  ListResourcesRequestSchema,
  ReadResourceRequestSchema,
  ListToolsRequestSchema,
  CallToolRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

// In-memory task store
const tasks = [
  { id: 1, title: "Review MCP documentation", completed: false },
  { id: 2, title: "Build sample server", completed: true },
];

const server = new Server(
  { name: "task-manager", version: "1.0.0" },
  { capabilities: { resources: {}, tools: {} } }
);

// Handle resource listing
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
  resources: [
    {
      uri: "tasks://all",
      name: "All Tasks",
      description: "List of all tasks",
    },
  ],
}));

// Handle resource reading
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
  if (request.params.uri === "tasks://all") {
    return {
      contents: [
        {
          uri: request.params.uri,
          mimeType: "application/json",
          text: JSON.stringify(tasks, null, 2),
        },
      ],
    };
  }
  throw new Error("Resource not found");
});

// Handle tool listing
server.setRequestHandler(ListToolsRequestSchema, async () => ({
  tools: [
    {
      name: "add_task",
      description: "Add a new task",
      inputSchema: {
        type: "object",
        properties: {
          title: { type: "string", description: "Task title" },
        },
        required: ["title"],
      },
    },
  ],
}));

// Handle tool execution
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === "add_task") {
    const { title } = request.params.arguments as { title: string };
    const newTask = {
      id: tasks.length + 1,
      title,
      completed: false,
    };
    tasks.push(newTask);
    return {
      content: [
        {
          type: "text",
          text: `Created task: ${title}`,
        },
      ],
    };
  }
  throw new Error("Tool not found");
});

// Start the server
async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
}

main().catch(console.error);

Configuration Files

package.json

1
2
3
4
5
6
7
{
  "type": "module",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  }
}

tsconfig.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "node",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true
  }
}

Build and Run

1
2
npm run build
npm start

Connecting to Claude

To use your MCP server with Claude Desktop, add it to your configuration:

1
2
3
4
5
6
7
8
{
  "mcpServers": {
    "task-manager": {
      "command": "node",
      "args": ["/path/to/my-mcp-server/dist/index.js"]
    }
  }
}

Now Claude can list your tasks and create new ones!


Best Practices

Security First

Always validate inputs and implement proper authentication before exposing tools to AI models. Never trust unvalidated input from any source.

Security Guidelines

ConcernRecommendationPriority
Input ValidationValidate all inputs against schemasCritical
AuthenticationImplement proper auth for sensitive operationsCritical
Audit LoggingLog all tool executionsHigh
Rate LimitingPrevent abuse and runaway operationsHigh

Performance Guidelines

AspectRecommendationBenefit
CachingCache frequently accessed resourcesLower latency
PaginationUse pagination for large datasetsMemory efficiency
TimeoutsImplement timeouts for long operationsReliability
BatchingBatch related operationsThroughput

Error Handling

PracticeImplementation
Clear MessagesProvide actionable error descriptions
Graceful HandlingHandle edge cases without crashing
No LeaksDon’t expose internal system details
LoggingLog errors for debugging

Integration Possibilities

CategoryExamplesUse Cases
DatabasesPostgreSQL, MongoDB, RedisData access, CRUD operations
Business AppsCRM, ERP, HelpdeskCustomer data, workflows
Dev ToolsGit, CI/CD, Issue trackersDevelopment automation
Cloud ServicesAWS, GCP, AzureInfrastructure management

Next Steps

  1. Identify Use Cases: What data and actions would be valuable for AI access?
  2. Design Resources: Define the data structures AI should query
  3. Plan Tools: Determine what actions AI should execute
  4. Implement Security: Build proper authentication and validation
  5. Test Thoroughly: Verify behavior with edge cases
  6. Deploy & Monitor: Track usage and errors in production

Need help building MCP integrations? Contact us to discuss your requirements.

Share this article
S

SmartTechLabs

Building Intelligent Solutions: IoT, Edge Computing, AI & LLM Integration