File size: 8,497 Bytes
ab38bab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?xml version="1.0" encoding="UTF-8"?>
<svg width="800px" height="420px" viewBox="0 0 800 420" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <style>
        .box {
            fill: #1e1e2e;
            stroke: #7e53f1;
            stroke-width: 2;
            rx: 8;
            ry: 8;
        }
        .title {
            font-family: 'Arial', sans-serif;
            font-size: 18px;
            font-weight: bold;
            fill: white;
            text-anchor: middle;
        }
        .code {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #e0e0e0;
        }
        .comment {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #7ca85a;
        }
        .keyword {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #569cd6;
        }
        .string {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #ce9178;
        }
        .function {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #dcdcaa;
        }
        .type {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #4ec9b0;
        }
        .variable {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 12px;
            fill: #9cdcfe;
        }
        .arrow {
            stroke: #7e53f1;
            stroke-width: 2;
            fill: none;
            marker-end: url(#arrowhead);
        }
        .label {
            font-family: 'Arial', sans-serif;
            font-size: 14px;
            font-weight: bold;
            fill: #7e53f1;
        }
    </style>
    
    <defs>
        <marker id="arrowhead" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
            <polygon points="0 0, 10 3.5, 0 7" fill="#7e53f1" />
        </marker>
    </defs>
    
    <!-- Title -->
    <text x="400" y="30" font-family="Arial" font-size="24" font-weight="bold" text-anchor="middle">MCP Server Implementation in TypeScript</text>
    
    <!-- Server Setup Code -->
    <rect x="50" y="60" width="340" height="160" class="box" />
    <text x="220" y="85" class="title">Server Setup</text>
    
    <text x="70" y="115" class="keyword">import</text>
    <text x="115" y="115" class="code">{ McpServer } </text>
    <text x="195" y="115" class="keyword">from</text>
    <text x="230" y="115" class="string">"@modelcontextprotocol/sdk/server/mcp.js"</text>
    <text x="480" y="115" class="code">;</text>
    
    <text x="70" y="135" class="keyword">import</text>
    <text x="115" y="135" class="code">{ StdioServerTransport } </text>
    <text x="285" y="135" class="keyword">from</text>
    <text x="320" y="135" class="string">"@modelcontextprotocol/sdk/server/stdio.js"</text>
    <text x="580" y="135" class="code">;</text>
    
    <text x="70" y="155" class="keyword">import</text>
    <text x="115" y="155" class="code">{ z } </text>
    <text x="130" y="155" class="keyword">from</text>
    <text x="165" y="155" class="string">"zod"</text>
    <text x="190" y="155" class="code">;</text>
    
    <text x="70" y="185" class="comment">// Create an MCP server</text>
    <text x="70" y="205" class="keyword">const</text>
    <text x="105" y="205" class="variable">server</text>
    <text x="145" y="205" class="code">= </text>
    <text x="155" y="205" class="keyword">new</text>
    <text x="180" y="205" class="type">McpServer</text>
    <text x="240" y="205" class="code">({</text>
    
    <text x="90" y="225" class="variable">name</text>
    <text x="120" y="225" class="code">: </text>
    <text x="130" y="225" class="string">"Demo"</text>
    <text x="165" y="225" class="code">,</text>
    
    <text x="90" y="245" class="variable">version</text>
    <text x="135" y="245" class="code">: </text>
    <text x="145" y="245" class="string">"1.0.0"</text>
    
    <text x="70" y="265" class="code">});</text>
    
    <!-- Tool Definition Code -->
    <rect x="50" y="240" width="340" height="160" class="box" />
    <text x="220" y="265" class="title">Tool Definition</text>
    
    <text x="70" y="295" class="comment">// Add a tool with Zod schema validation</text>
    <text x="70" y="315" class="variable">server</text>
    <text x="110" y="315" class="code">.</text>
    <text x="115" y="315" class="function">tool</text>
    <text x="140" y="315" class="code">(</text>
    
    <text x="90" y="335" class="string">"add"</text>
    <text x="120" y="335" class="code">,</text>
    
    <text x="90" y="355" class="code">{ </text>
    <text x="100" y="355" class="variable">a</text>
    <text x="105" y="355" class="code">: </text>
    <text x="110" y="355" class="variable">z</text>
    <text x="117" y="355" class="code">.</text>
    <text x="122" y="355" class="function">number</text>
    <text x="165" y="355" class="code">(), </text>
    <text x="175" y="355" class="variable">b</text>
    <text x="180" y="355" class="code">: </text>
    <text x="185" y="355" class="variable">z</text>
    <text x="192" y="355" class="code">.</text>
    <text x="197" y="355" class="function">number</text>
    <text x="240" y="355" class="code">() },</text>
    
    <text x="90" y="375" class="keyword">async</text>
    <text x="125" y="375" class="code">({ </text>
    <text x="135" y="375" class="variable">a</text>
    <text x="142" y="375" class="code">, </text>
    <text x="150" y="375" class="variable">b</text>
    <text x="157" y="375" class="code"> }) => ({</text>
    
    <text x="110" y="395" class="variable">content</text>
    <text x="155" y="395" class="code">: [{ </text>
    <text x="170" y="395" class="variable">type</text>
    <text x="195" y="395" class="code">: </text>
    <text x="205" y="395" class="string">"text"</text>
    <text x="240" y="395" class="code">, </text>
    <text x="250" y="395" class="variable">text</text>
    <text x="275" y="395" class="code">: </text>
    <text x="285" y="395" class="function">String</text>
    <text x="320" y="395" class="code">(</text>
    <text x="325" y="395" class="variable">a</text>
    <text x="332" y="395" class="code"> + </text>
    <text x="342" y="395" class="variable">b</text>
    <text x="349" y="395" class="code">) }]</text>
    
    <text x="70" y="415" class="code">}));</text>
    
    <!-- Server Connection Code -->
    <rect x="410" y="60" width="340" height="120" class="box" />
    <text x="580" y="85" class="title">Server Connection</text>
    
    <text x="430" y="115" class="comment">// Connect using STDIO transport</text>
    <text x="430" y="135" class="keyword">const</text>
    <text x="465" y="135" class="variable">transport</text>
    <text x="525" y="135" class="code">= </text>
    <text x="535" y="135" class="keyword">new</text>
    <text x="560" y="135" class="type">StdioServerTransport</text>
    <text x="685" y="135" class="code">();</text>
    
    <text x="430" y="155" class="keyword">await</text>
    <text x="465" y="155" class="variable">server</text>
    <text x="505" y="155" class="code">.</text>
    <text x="510" y="155" class="function">connect</text>
    <text x="555" y="155" class="code">(</text>
    <text x="560" y="155" class="variable">transport</text>
    <text x="615" y="155" class="code">);</text>
    
    <!-- Flow Diagram -->
    <rect x="410" y="200" width="340" height="200" class="box" />
    <text x="580" y="225" class="title">Runtime Flow</text>
    
    <!-- MCP Flow -->
    <rect x="450" y="250" width="120" height="40" fill="#4a6cf7" rx="5" ry="5" opacity="0.7" />
    <text x="510" y="275" font-family="Arial" font-size="14" fill="white" text-anchor="middle">AI Application</text>
    
    <rect x="450" y="350" width="120" height="40" fill="#7e53f1" rx="5" ry="5" opacity="0.7" />
    <text x="510" y="375" font-family="Arial" font-size="14" fill="white" text-anchor="middle">MCP Server</text>
    
    <rect x="600" y="350" width="120" height="40" fill="#ff594f" rx="5" ry="5" opacity="0.7" />
    <text x="660" y="375" font-family="Arial" font-size="14" fill="white" text-anchor="middle">External Tool</text>
    
    <!-- Arrows -->
    <path d="M 510 290 L 510 350" class="arrow" />
    <path d="M 570 370 L 600 370" class="arrow" />
    
    <text x="480" y="320" class="label">1. Call</text>
    <text x="585" y="360" class="label">2. Execute</text>
</svg>