Typescript Mcp Server Usage
Okay, I will provide you with a basic example of how to create an MCP (Minecraft Protocol) server using TypeScript. Keep in mind that building a full-fledged Minecraft server from scratch is a complex undertaking. This example will focus on the core networking and handshake aspects. You'll need to install some dependencies first. **1. Project Setup and Dependencies:** First, create a new TypeScript project: ```bash mkdir mcp-server cd mcp-server npm init -y npm install typescript ts-node ws --save npm install @types/node @types/ws --save-dev ``` * `ws`: A popular WebSocket library for Node.js. Minecraft uses a custom protocol over TCP, but WebSocket provides a convenient way to handle the underlying socket communication for this example. In a real Minecraft server, you'd implement the protocol directly over TCP. * `typescript`: The TypeScript compiler. * `ts-node`: Allows you to execute TypeScript files directly. * `@types/node`, `@types/ws`: TypeScript type definitions for Node.js and WebSocket, respectively. Create a `tsconfig.json` file: ```json { "compilerOptions": { "target": "es2017", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true }, "include": ["src/**/*"], "exclude": ["node_modules"] } ``` **2. `src/index.ts` (Main Server Code):** ```typescript import * as WebSocket from 'ws'; const PORT = 25565; // Or any port you prefer const wss = new WebSocket.Server({ port: PORT }, () => { console.log(`Server started on port ${PORT}`); }); wss.on('connection', ws => { console.log('Client connected'); ws.on('message', message => { // In a real Minecraft server, you'd parse the Minecraft protocol packets here. // This is a simplified example, so we're just echoing the message. console.log(`Received: ${message}`); // Example: Echo the message back to the client ws.send(`Server received: ${message}`); }); ws.on('close', () => { console.log('Client disconnected'); }); ws.on('error', error => { console.error('WebSocket error:', error); }); }); wss.on('error', error => { console.error('Server error:', error); }); console.log('MCP Server is starting...'); ``` **Explanation:** * **Import `ws`:** Imports the WebSocket library. * **`PORT`:** Defines the port the server will listen on. Minecraft's default port is 25565. * **`WebSocket.Server`:** Creates a new WebSocket server instance. * **`wss.on('connection', ...)`:** This is the core of the server. It's called whenever a new client connects. * `ws`: Represents the WebSocket connection to the client. * `ws.on('message', ...)`: Handles incoming messages from the client. **This is where you would implement the Minecraft protocol parsing and handling.** In this example, it simply logs the message and echoes it back. * `ws.on('close', ...)`: Handles client disconnections. * `ws.on('error', ...)`: Handles errors on the WebSocket connection. * **`wss.on('error', ...)`:** Handles errors on the server itself. **3. Building and Running:** 1. **Compile:** Run `npm run tsc` (or just `tsc` if you have it globally installed) to compile the TypeScript code into JavaScript. This will create a `dist` directory with the compiled `index.js` file. You might need to add `"build": "tsc"` to your `package.json` scripts section. 2. **Run:** Execute the server using `node dist/index.js` or `ts-node src/index.ts`. **Important Considerations and Next Steps (for a *real* Minecraft server):** * **Minecraft Protocol:** The code above uses WebSockets for simplicity. A *real* Minecraft server communicates using a custom binary protocol over TCP. You'll need to: * **Understand the Protocol:** Study the Minecraft protocol documentation (see links below). It involves packets with specific IDs, data types, and structures. * **Implement Packet Parsing/Serialization:** Write code to read incoming TCP data, parse it into Minecraft packets, and serialize packets to send back to the client. Libraries exist that can help with this, but you'll still need to understand the protocol. * **Handshake:** The initial connection involves a handshake where the client and server exchange protocol versions. You need to implement this handshake correctly. * **Authentication:** Minecraft servers typically require authentication. You'll need to handle login requests and verify user credentials. * **Game Logic:** This example has *no* game logic. You'll need to implement the core game mechanics: world generation, player movement, entity management, block updates, etc. * **Threading/Asynchronous Operations:** Minecraft servers are highly concurrent. You'll need to use threading or asynchronous programming (e.g., `async/await` in TypeScript) to handle multiple clients efficiently. * **World Storage:** You'll need a way to store the game world data (blocks, entities, etc.). This could involve files, databases, or other storage mechanisms. **Example of a very basic handshake (to illustrate the concept):** ```typescript // Inside the 'connection' handler: ws.on('message', message => { const buffer = Buffer.from(message); // Convert to Buffer for binary data // Example: Very basic handshake (replace with actual protocol parsing) if (buffer[0] === 0x00) { // Assuming 0x00 is a handshake packet ID const protocolVersion = buffer.readInt32BE(1); // Read protocol version const serverAddressLength = buffer.readInt8(5); const serverAddress = buffer.toString('utf8', 6, 6 + serverAddressLength); const serverPort = buffer.readUInt32BE(6 + serverAddressLength); const nextState = buffer.readInt8(10 + serverAddressLength); console.log(`Handshake: Protocol ${protocolVersion}, Address ${serverAddress}:${serverPort}, Next State ${nextState}`); // Send a response (e.g., a status response) const response = JSON.stringify({ version: { name: "My TypeScript Server", protocol: protocolVersion }, players: { max: 10, online: 0, sample: [] }, description: { text: "A simple TypeScript Minecraft server" } }); const responseBuffer = Buffer.from(response); const length = responseBuffer.length; const lengthBuffer = Buffer.alloc(1); lengthBuffer.writeInt8(length); ws.send(Buffer.concat([lengthBuffer, responseBuffer])); } else { console.log(`Received other message: ${message}`); } }); ``` **Important Resources:** * **Minecraft Protocol Documentation:** This is essential. Search for "Minecraft Protocol" or "wiki.vg Minecraft Protocol". `wiki.vg` is a good starting point. * **Existing Minecraft Server Implementations:** Look at open-source Minecraft server projects (e.g., in Java or other languages) to see how they handle the protocol and game logic. This can be a great learning resource. However, be aware that the protocol changes over time, so make sure the implementation you're looking at is relatively up-to-date. **Chinese Translation of Key Terms:** * **Minecraft Protocol:** 我的世界协议 (Wǒ de shìjiè xiéyì) * **Server:** 服务器 (Fúwùqì) * **Client:** 客户端 (Kèhùduān) * **Packet:** 数据包 (Shùjù bāo) * **Handshake:** 握手 (Wòshǒu) * **Authentication:** 身份验证 (Shēnfèn yànzhèng) * **WebSocket:** WebSocket (Websocket, no direct translation is commonly used) * **TCP:** TCP (TCP, no direct translation is commonly used) * **Port:** 端口 (Duānkǒu) * **Connection:** 连接 (Liánjiē) * **Message:** 消息 (Xiāoxī) * **Error:** 错误 (Cuòwù) * **Protocol Version:** 协议版本 (Xiéyì bǎnběn) This is a very basic starting point. Building a real Minecraft server is a significant project. Good luck!
americano212
README
docker build -t mcp/test -f ./Dockerfile .
{
"mcpServers": {
"tester": {
"command": "docker",
"args": ["run", "-i", "--rm", "mcp/test"],
"env": {
"TEST": "test"
}
}
}
}
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。
Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。
Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。