@tiangong-lca/mcp-server 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 linancn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # TianGong-LCA-MCP
2
+
3
+ [中文](./README.md) | [English](./README_EN.md)
4
+
5
+ TianGong LCA Model Context Protocol (MCP) Server 支持 STDIO 和 SSE 两种协议。
6
+
7
+ ## 启动 MCP 服务器
8
+
9
+ ### 客户端 STDIO 服务器
10
+
11
+ ```bash
12
+ npm install -g @tiangong-lca/mcp-server
13
+
14
+ npx dotenv -e .env -- \
15
+ npx @tiangong-lca/mcp-server
16
+ ```
17
+
18
+ ### 远程 SSE 服务器
19
+
20
+ ```bash
21
+ npm install -g @tiangong-lca/mcp-server
22
+ npm install -g supergateway
23
+
24
+ npx dotenv -e .env -- \
25
+ npx -y supergateway \
26
+ --stdio "npx -y @tiangong-lca/mcp-server" \
27
+ --port 3001 \
28
+ --ssePath /sse --messagePath /message
29
+ ```
30
+
31
+ ### 使用 Docker
32
+
33
+ ```bash
34
+ # 使用 Dockerfile 构建 MCP 服务器镜像(可选)
35
+ docker build -t linancn/tiangong-lca-mcp-server:0.0.1 .
36
+
37
+ # 拉取 MCP 服务器镜像
38
+ docker pull linancn/tiangong-lca-mcp-server:0.0.1
39
+
40
+ # 使用 Docker 启动 MCP 服务器
41
+ docker run -d \
42
+ --name tiangong-lca-mcp-server \
43
+ --publish 3001:80 \
44
+ --env-file .env \
45
+ linancn/tiangong-lca-mcp-server:0.0.1
46
+ ```
47
+
48
+ ## 开发
49
+
50
+ ### 环境设置
51
+
52
+ ```bash
53
+ # 安装 Node.js
54
+ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
55
+ nvm install 22
56
+ nvm use
57
+
58
+ # 安装依赖
59
+ npm install
60
+
61
+ # 更新依赖
62
+ npm update && npm ci
63
+ ```
64
+
65
+ ### 代码格式化
66
+
67
+ ```bash
68
+ # 使用代码检查工具格式化代码
69
+ npm run lint
70
+ ```
71
+
72
+ ### 本地测试
73
+
74
+ #### STDIO 服务器
75
+
76
+ ```bash
77
+ # 使用 MCP Inspector 启动 STDIO 服务器
78
+ npm start
79
+ ```
80
+
81
+ #### SSE 服务器
82
+
83
+ ```bash
84
+ # 打包当前项目
85
+ npm run build && npm pack
86
+
87
+ # 如果需要可以全局安装 supergateway(可选)
88
+ npm install -g supergateway
89
+
90
+ # 启动 SSE 服务器,如配置了参数 --baseUrl ,应设置为有效的 IP 地址或域名
91
+ npx dotenv -e .env -- \
92
+ npx -y supergateway \
93
+ --stdio "npx -y tiangong-lca-mcp-server-0.0.1.tgz" \
94
+ --port 3001 \
95
+ --ssePath /sse \
96
+ --messagePath /message
97
+
98
+ # 启动 MCP Inspector
99
+ npx @modelcontextprotocol/inspector
100
+ ```
101
+
102
+ ### 发布
103
+
104
+ ```bash
105
+ npm login
106
+
107
+ npm run build && npm publish
108
+ ```
@@ -0,0 +1,22 @@
1
+ const cleanObject = (obj) => {
2
+ if (Array.isArray(obj)) {
3
+ return obj
4
+ .map((v) => (typeof v === 'object' ? cleanObject(v) : v))
5
+ .filter((v) => v !== undefined && v !== null);
6
+ }
7
+ else if (typeof obj === 'object' && obj !== null) {
8
+ const cleanedObj = Object.entries(obj)
9
+ .map(([k, v]) => [k, cleanObject(v)])
10
+ .reduce((acc, [k, v]) => {
11
+ if (v !== undefined &&
12
+ v !== null &&
13
+ (typeof v !== 'object' || (typeof v === 'object' && Object.keys(v).length > 0))) {
14
+ acc[k] = v;
15
+ }
16
+ return acc;
17
+ }, {});
18
+ return cleanedObj;
19
+ }
20
+ return obj;
21
+ };
22
+ export default cleanObject;
@@ -0,0 +1,4 @@
1
+ export const base_url = process.env.BASE_URL ?? '';
2
+ export const supabase_anon_key = process.env.SUPABASE_ANON_KEY ?? '';
3
+ export const x_api_key = process.env.X_API_KEY ?? '';
4
+ export const x_region = process.env.X_REGION ?? '';
@@ -0,0 +1,13 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { regFlowSearchTool } from '../tools/flow_hybrid_search.js';
3
+ import { regProcessSearchTool } from '../tools/process_hybrid_search.js';
4
+ export function initializeServer() {
5
+ const server = new McpServer({
6
+ name: 'TianGong-MCP-Server',
7
+ version: '1.0.0',
8
+ });
9
+ regFlowSearchTool(server);
10
+ regProcessSearchTool(server);
11
+ return server;
12
+ }
13
+ export const server = initializeServer();
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
4
+ import { regFlowSearchTool } from './tools/flow_hybrid_search.js';
5
+ import { regProcessSearchTool } from './tools/process_hybrid_search.js';
6
+ const server = new McpServer({
7
+ name: 'TianGong-MCP-Server',
8
+ version: '1.0.0',
9
+ });
10
+ regFlowSearchTool(server);
11
+ regProcessSearchTool(server);
12
+ async function runServer() {
13
+ const transport = new StdioServerTransport();
14
+ await server.connect(transport);
15
+ }
16
+ runServer().catch((error) => {
17
+ console.error('Fatal error running server:', error);
18
+ process.exit(1);
19
+ });
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env node
2
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
3
+ import express from 'express';
4
+ import { server } from './_shared/init_server.js';
5
+ const app = express();
6
+ app.use(express.json());
7
+ app.post('/mcp', async (req, res) => {
8
+ try {
9
+ const transport = new StreamableHTTPServerTransport({
10
+ sessionIdGenerator: undefined,
11
+ });
12
+ await server.connect(transport);
13
+ await transport.handleRequest(req, res, req.body);
14
+ res.on('close', () => {
15
+ console.log('Request closed');
16
+ transport.close();
17
+ server.close();
18
+ });
19
+ }
20
+ catch (error) {
21
+ console.error('Error handling MCP request:', error);
22
+ if (!res.headersSent) {
23
+ res.status(500).json({
24
+ jsonrpc: '2.0',
25
+ error: {
26
+ code: -32603,
27
+ message: 'Internal server error',
28
+ },
29
+ id: null,
30
+ });
31
+ }
32
+ }
33
+ });
34
+ app.get('/mcp', async (req, res) => {
35
+ console.log('Received GET MCP request');
36
+ res.writeHead(405).end(JSON.stringify({
37
+ jsonrpc: '2.0',
38
+ error: {
39
+ code: -32000,
40
+ message: 'Method not allowed.',
41
+ },
42
+ id: null,
43
+ }));
44
+ });
45
+ app.delete('/mcp', async (req, res) => {
46
+ console.log('Received DELETE MCP request');
47
+ res.writeHead(405).end(JSON.stringify({
48
+ jsonrpc: '2.0',
49
+ error: {
50
+ code: -32000,
51
+ message: 'Method not allowed.',
52
+ },
53
+ id: null,
54
+ }));
55
+ });
56
+ const PORT = Number(process.env.PORT ?? 9278);
57
+ const HOST = process.env.HOST ?? '0.0.0.0';
58
+ app.listen(PORT, HOST, () => {
59
+ console.log(`MCP Stateless Streamable HTTP Server listening on port ${PORT}`);
60
+ });
@@ -0,0 +1,22 @@
1
+ const cleanObject = (obj) => {
2
+ if (Array.isArray(obj)) {
3
+ return obj
4
+ .map((v) => (typeof v === 'object' ? cleanObject(v) : v))
5
+ .filter((v) => v !== undefined && v !== null);
6
+ }
7
+ else if (typeof obj === 'object' && obj !== null) {
8
+ const cleanedObj = Object.entries(obj)
9
+ .map(([k, v]) => [k, cleanObject(v)])
10
+ .reduce((acc, [k, v]) => {
11
+ if (v !== undefined &&
12
+ v !== null &&
13
+ (typeof v !== 'object' || (typeof v === 'object' && Object.keys(v).length > 0))) {
14
+ acc[k] = v;
15
+ }
16
+ return acc;
17
+ }, {});
18
+ return cleanedObj;
19
+ }
20
+ return obj;
21
+ };
22
+ export default cleanObject;
@@ -0,0 +1,4 @@
1
+ export const base_url = process.env.BASE_URL ?? '';
2
+ export const supabase_anon_key = process.env.SUPABASE_ANON_KEY ?? '';
3
+ export const x_api_key = process.env.X_API_KEY ?? '';
4
+ export const x_region = process.env.X_REGION ?? '';
@@ -0,0 +1,47 @@
1
+ import { z } from 'zod';
2
+ import cleanObject from '../_shared/clean_object.js';
3
+ import { base_url, supabase_anon_key, x_api_key, x_region } from '../_shared/config.js';
4
+ const input_schema = {
5
+ query: z.string().min(1).describe('Queries from user'),
6
+ };
7
+ async function searchFlows({ query }) {
8
+ const url = `${base_url}/flow_hybrid_search`;
9
+ try {
10
+ const response = await fetch(url, {
11
+ method: 'POST',
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ Authorization: `Bearer ${supabase_anon_key}`,
15
+ 'x-api-key': x_api_key,
16
+ 'x-region': x_region,
17
+ },
18
+ body: JSON.stringify(cleanObject({
19
+ query,
20
+ })),
21
+ });
22
+ if (!response.ok) {
23
+ throw new Error(`HTTP error: ${response.status} ${response.statusText}`);
24
+ }
25
+ const data = await response.json();
26
+ return JSON.stringify(data);
27
+ }
28
+ catch (error) {
29
+ console.error('Error making the request:', error);
30
+ throw error;
31
+ }
32
+ }
33
+ export function regFlowSearchTool(server) {
34
+ server.tool('Search_flows_Tool', 'Search LCA flows data.', input_schema, async ({ query }) => {
35
+ const result = await searchFlows({
36
+ query,
37
+ });
38
+ return {
39
+ content: [
40
+ {
41
+ type: 'text',
42
+ text: result,
43
+ },
44
+ ],
45
+ };
46
+ });
47
+ }
@@ -0,0 +1,47 @@
1
+ import { z } from 'zod';
2
+ import cleanObject from '../_shared/clean_object.js';
3
+ import { base_url, supabase_anon_key, x_api_key, x_region } from '../_shared/config.js';
4
+ const input_schema = {
5
+ query: z.string().min(1).describe('Queries from user'),
6
+ };
7
+ async function searchProcesses({ query }) {
8
+ const url = `${base_url}/process_hybrid_search`;
9
+ try {
10
+ const response = await fetch(url, {
11
+ method: 'POST',
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ Authorization: `Bearer ${supabase_anon_key}`,
15
+ 'x-api-key': x_api_key,
16
+ 'x-region': x_region,
17
+ },
18
+ body: JSON.stringify(cleanObject({
19
+ query,
20
+ })),
21
+ });
22
+ if (!response.ok) {
23
+ throw new Error(`HTTP error: ${response.status} ${response.statusText}`);
24
+ }
25
+ const data = await response.json();
26
+ return JSON.stringify(data);
27
+ }
28
+ catch (error) {
29
+ console.error('Error making the request:', error);
30
+ throw error;
31
+ }
32
+ }
33
+ export function regProcessSearchTool(server) {
34
+ server.tool('Search_processes_Tool', 'Search LCA processes data.', input_schema, async ({ query }) => {
35
+ const result = await searchProcesses({
36
+ query,
37
+ });
38
+ return {
39
+ content: [
40
+ {
41
+ type: 'text',
42
+ text: result,
43
+ },
44
+ ],
45
+ };
46
+ });
47
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@tiangong-lca/mcp-server",
3
+ "version": "0.0.2",
4
+ "description": "TianGong LCA MCP Server",
5
+ "license": "MIT",
6
+ "author": "Nan LI",
7
+ "type": "module",
8
+ "bin": {
9
+ "tiangong-lca-mcp-stdio": "dist/src/index.js",
10
+ "tiangong-lca-mcp-http": "dist/src/index_server.js"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "scripts": {
19
+ "build": "tsc && shx chmod +x dist/src/*.js",
20
+ "start": "npm run build && npx dotenv -e .env -- npx @modelcontextprotocol/inspector node dist/src/index.js",
21
+ "start:server": "npm run build && npx dotenv -e .env -- node dist/src/index_server.js",
22
+ "lint": "prettier -c --write \"**/**.{js,jsx,tsx,ts,less,md,json}\""
23
+ },
24
+ "dependencies": {
25
+ "@types/express": "^5.0.1",
26
+ "@modelcontextprotocol/sdk": "^1.10.2",
27
+ "@supabase/supabase-js": "^2.49.4",
28
+ "zod": "^3.24.3"
29
+ },
30
+ "devDependencies": {
31
+ "@modelcontextprotocol/inspector": "^0.10.2",
32
+ "dotenv-cli": "^8.0.0",
33
+ "prettier": "^3.5.3",
34
+ "prettier-plugin-organize-imports": "^4.1.0",
35
+ "shx": "^0.4.0",
36
+ "supergateway": "^2.7.0",
37
+ "typescript": "^5.8.3"
38
+ }
39
+ }