my-mcp-server-251127
服务介绍
TypeScript MCP Server
TypeScript MCP SDK Model Context Protocol (MCP) .
typescript-mcp-server-boilerplate/
src/
index.ts # MCP
build/ # JavaScript ( )
package.json #
tsconfig.json # TypeScript
README.md #
1.
npm install
2.
src/index.ts :
const server = new McpServer({
name: 'typescript-mcp-server', //
version: '1.0.0',
//
capabilities: {
tools: {},
resources: {}
}
})
****: , .
3.
npm run build
4.
node build/index.js
build/ JavaScript , MCP .
MCP (Tool)
MCP server.tool() **Zod ** :
import { z } from 'zod'
//
server.tool(
'calculator',
{
operation: z
.enum(['add', 'subtract', 'multiply', 'divide'])
.describe(' (add, subtract, multiply, divide)'),
a: z.number().describe(' '),
b: z.number().describe(' ')
},
async ({ operation, a, b }) => {
//
let result: number
switch (operation) {
case 'add':
result = a + b
break
case 'subtract':
result = a - b
break
case 'multiply':
result = a * b
break
case 'divide':
if (b === 0) throw new Error('0 ')
result = a / b
break
default:
throw new Error(' ')
}
const operationSymbols = {
add: '+',
subtract: '-',
multiply: '',
divide: ''
} as const
const operationSymbol =
operationSymbols[operation as keyof typeof operationSymbols]
return {
content: [
{
type: 'text',
text: `${a} ${operationSymbol} ${b} = ${result}`
}
]
}
}
)
//
server.tool(
'get_weather',
{
city: z.string().describe(' '),
unit: z
.enum(['celsius', 'fahrenheit'])
.optional()
.default('celsius')
.describe(' (: celsius)')
},
async ({ city, unit }) => {
try {
// API ()
const weatherData = await fetchWeatherData(city, unit)
return {
content: [
{
type: 'text',
text: `${city} :
: ${weatherData.temperature}${unit === 'celsius' ? 'C' : 'F'}
: ${weatherData.condition}
: ${weatherData.humidity}%
: ${weatherData.windSpeed}km/h`
}
]
}
} catch (error) {
throw new Error(
` : ${(error as Error).message}`
)
}
}
)
//
async function fetchWeatherData(city: string, unit: string) {
// API
//
return {
temperature: unit === 'celsius' ? 22 : 72,
condition: '',
humidity: 65,
windSpeed: 12
}
}
MCP :
//
server.resource(
'example-file',
'file://example.txt',
{
name: ' ',
description: ' ',
mimeType: 'text/plain'
},
async () => {
return {
contents: [
{
uri: 'file://example.txt',
mimeType: 'text/plain',
text: ' .'
}
]
}
}
)
//
server.resource(
'app-settings',
'config://settings',
{
name: ' ',
description: ' ',
mimeType: 'application/json'
},
async () => {
const settings = {
theme: 'dark',
language: 'ko-KR',
notifications: true,
lastUpdated: new Date().toISOString()
}
return {
contents: [
{
uri: 'config://settings',
mimeType: 'application/json',
text: JSON.stringify(settings, null, 2)
}
]
}
}
)
- @modelcontextprotocol/sdk: MCP SDK
- zod: TypeScript
- typescript: TypeScript
npm run build: TypeScript JavaScript
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { z } from 'zod'
//
const server = new McpServer({
name: 'my-mcp-server',
version: '1.0.0',
capabilities: {
tools: {},
resources: {}
}
})
//
server.tool(
'greet',
{
name: z.string().describe(' '),
language: z
.enum(['ko', 'en'])
.optional()
.default('ko')
.describe(' (: ko)')
},
async ({ name, language }) => {
const greeting =
language === 'ko' ? `, ${name}!` : `Hello, ${name}!`
return {
content: [
{
type: 'text',
text: greeting
}
]
}
}
)
//
server.resource(
'system-info',
'system://info',
{
name: ' ',
description: ' ',
mimeType: 'application/json'
},
async () => {
const systemInfo = {
server: 'my-mcp-server',
version: '1.0.0',
timestamp: new Date().toISOString(),
uptime: process.uptime()
}
return {
contents: [
{
uri: 'system://info',
mimeType: 'application/json',
text: JSON.stringify(systemInfo, null, 2)
}
]
}
}
)
//
async function main() {
const transport = new StdioServerTransport()
await server.connect(transport)
console.error('MCP ')
}
main().catch(console.error)
Cursor MCP
MCP Cursor :
./.cursor/mcp.json :
{
"mcpServers": {
"typescript-mcp-server": {
"command": "node",
"args": ["/ABSOLUTE/PATH/TO/YOUR/PROJECT/build/index.js"]
}
}
}
****: .
pwd.
Cursor MCP :
- "5 3 ?" ( )
- " " ( )
MIT