aws-lambda-mcp-server 0.0.0

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/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # aws-lambda-mcp-server
2
+
3
+ AWS Lambda上でMCP(Model Context Protocol)サーバー機能を提供するためのライブラリです。
4
+ APIエンドポイントの実装や、各種AWSサービスとの連携を簡易化することを目的としています。
5
+
6
+ ## ライブラリの使い方
7
+
8
+ ### インストール
9
+
10
+ ```sh
11
+ pnpm add aws-lambda-mcp-server
12
+ # または
13
+ npm install aws-lambda-mcp-server
14
+ ```
15
+
16
+ ### 基本的な利用例
17
+
18
+ TypeScriptでの利用例です。
19
+
20
+ ```typescript
21
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
22
+ import { handler } from 'aws-lambda-mcp-server';
23
+ import { handle } from 'hono/aws-lambda';
24
+
25
+ // MCPサーバーのインスタンスを作成
26
+ const server = new McpServer({
27
+ name: 'my-mcp-server',
28
+ version: '1.0.0',
29
+ });
30
+
31
+ // MCPサーバーのインスタンスにToolsやResourcesなどを設定する
32
+ server.tool(
33
+ "say_hello",
34
+ { who: z.string() },
35
+ async ({ who }) => ({
36
+ content: [{
37
+ type: "text",
38
+ text: `${who} さん、こんにちは!`
39
+ }]
40
+ })
41
+ );
42
+
43
+ // Hono アプリケーションを作成
44
+ const app = createHonoApp(server);
45
+
46
+ // AWS Lambdaのエントリポイントとして利用
47
+ export const handler = handle(app);
48
+ ```
49
+
50
+ Lambdaの設定で `handler` をエントリポイントに指定してください。
@@ -0,0 +1,72 @@
1
+ import { Config, defineConfig } from 'eslint/config';
2
+ import eslint from '@eslint/js';
3
+ import { configs, parser } from 'typescript-eslint';
4
+ import stylistic from '@stylistic/eslint-plugin';
5
+ import importPlugin from 'eslint-plugin-import';
6
+ // @ts-expect-error ignore type errors
7
+ import pluginPromise from 'eslint-plugin-promise';
8
+
9
+ import { includeIgnoreFile } from '@eslint/compat';
10
+ import path from 'node:path';
11
+ import { fileURLToPath } from 'node:url';
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+ const gitignorePath = path.resolve(__dirname, '.gitignore');
16
+
17
+ const eslintConfig: Config[] = defineConfig(
18
+ {
19
+ ignores: [
20
+ ...(includeIgnoreFile(gitignorePath).ignores || []),
21
+ '**/*.d.ts',
22
+ 'src/tsconfig.json',
23
+ 'src/stories',
24
+ '**/*.css',
25
+ 'node_modules/**/*',
26
+ 'out',
27
+ 'cdk.out',
28
+ 'dist',
29
+ 'app',
30
+ ],
31
+ },
32
+ eslint.configs.recommended,
33
+ configs.strict,
34
+ configs.stylistic,
35
+ pluginPromise.configs['flat/recommended'],
36
+ {
37
+ files: ['**/*.ts'],
38
+ plugins: {
39
+ '@stylistic': stylistic,
40
+ },
41
+ languageOptions: {
42
+ ecmaVersion: 'latest',
43
+ sourceType: 'module',
44
+ parser,
45
+ parserOptions: {
46
+ tsconfigRootDir: __dirname,
47
+ project: ['./tsconfig-eslint.json'],
48
+ },
49
+ },
50
+ extends: [
51
+ importPlugin.flatConfigs.recommended,
52
+ importPlugin.flatConfigs.typescript,
53
+ ],
54
+ settings: {
55
+ 'import/resolver': {
56
+ // You will also need to install and configure the TypeScript resolver
57
+ // See also https://github.com/import-js/eslint-import-resolver-typescript#configuration
58
+ 'typescript': true,
59
+ 'node': true,
60
+ },
61
+ },
62
+ rules: {
63
+ '@stylistic/semi': ['error', 'always'],
64
+ '@stylistic/indent': ['error', 2],
65
+ '@stylistic/comma-dangle': ['error', 'always-multiline'],
66
+ '@stylistic/arrow-parens': ['error', 'always'],
67
+ '@stylistic/quotes': ['error', 'single'],
68
+ },
69
+ },
70
+ );
71
+
72
+ export default eslintConfig;
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "aws-lambda-mcp-server",
3
+ "version": "0.0.0",
4
+ "description": "A Hono wrapper for building an MCP (Model Context Protocol) Server that runs on AWS Lambda functions.",
5
+ "type": "module",
6
+ "keywords": [
7
+ "mcp-server",
8
+ "modelcontextprotocol",
9
+ "mcp",
10
+ "lambda"
11
+ ],
12
+ "private": false,
13
+ "sideEffects": false,
14
+ "homepage": "https://github.com/poad/aws-lambda-mcp-server#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/poad/aws-lambda-mcp-server.git"
18
+ },
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "module": "./dist/index.js",
26
+ "types": "dist/index.d.ts",
27
+ "main": "./dist/index.js",
28
+ "author": "poad",
29
+ "license": "ISC",
30
+ "scripts": {
31
+ "build": "tsc",
32
+ "watch": "tsc -w",
33
+ "test": "vitest run --passWithNoTests",
34
+ "lint": "eslint .",
35
+ "lint-fix": "eslint . --fix"
36
+ },
37
+ "devDependencies": {
38
+ "@eslint/compat": "^1.4.1",
39
+ "@eslint/js": "^9.39.0",
40
+ "@stylistic/eslint-plugin": "^5.5.0",
41
+ "@types/node": "24.9.2",
42
+ "eslint": "^9.39.0",
43
+ "eslint-import-resolver-typescript": "^4.4.4",
44
+ "eslint-plugin-import": "^2.32.0",
45
+ "eslint-plugin-promise": "^7.2.1",
46
+ "jiti": "^2.6.1",
47
+ "tsx": "^4.20.6",
48
+ "typescript": "^5.9.3",
49
+ "typescript-eslint": "^8.46.2",
50
+ "vitest": "^4.0.6"
51
+ },
52
+ "dependencies": {
53
+ "@aws-lambda-powertools/logger": "^2.28.1",
54
+ "@hono/mcp": "^0.1.5",
55
+ "@modelcontextprotocol/sdk": "^1.20.2",
56
+ "hono": "^4.10.4",
57
+ "zod": "^3.25.76"
58
+ }
59
+ }
package/src/index.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { Logger } from '@aws-lambda-powertools/logger';
2
+ import { StreamableHTTPTransport } from '@hono/mcp';
3
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
+ import { Context, Hono } from 'hono';
5
+ import { BlankEnv, BlankInput } from 'hono/types';
6
+
7
+ const logger = new Logger();
8
+
9
+ const methodNotAllowedHandler = async (
10
+ c: Context<BlankEnv, '/mcp', BlankInput>,
11
+ ) => {
12
+ return c.json(
13
+ {
14
+ jsonrpc: '2.0',
15
+ error: {
16
+ code: -32000,
17
+ message: 'メソッドは許可されていません。',
18
+ },
19
+ id: null,
20
+ },
21
+ { status: 405 },
22
+ );
23
+ };
24
+
25
+ const handleError = (
26
+ c: Context<BlankEnv, '/mcp', BlankInput>,
27
+ reason: unknown,
28
+ logMessage: string,
29
+ ) => {
30
+ const errorDetails = reason instanceof Error
31
+ ? { message: reason.message, stack: reason.stack, name: reason.name }
32
+ : { reason };
33
+ logger.error(logMessage, errorDetails);
34
+ return c.json(
35
+ {
36
+ jsonrpc: '2.0',
37
+ error: {
38
+ code: -32603,
39
+ message: '内部サーバーエラー',
40
+ },
41
+ id: null,
42
+ },
43
+ { status: 500 },
44
+ );
45
+ };
46
+
47
+ const closeResources = async (server: McpServer, transport: StreamableHTTPTransport) => {
48
+ // 両方のクローズを確実に実行(片方が失敗してももう片方を実行)
49
+ const closeResults = await Promise.allSettled([
50
+ transport.close(),
51
+ server.close(),
52
+ ]);
53
+
54
+ // クローズエラーをログ出力
55
+ closeResults.forEach((result, index) => {
56
+ if (result.status === 'rejected') {
57
+ const resourceName = index === 0 ? 'transport' : 'server';
58
+ const error = result.reason;
59
+ const errorDetails = error instanceof Error
60
+ ? { message: error.message, stack: error.stack }
61
+ : error;
62
+ logger.error(`Error closing ${resourceName}:`, { error: errorDetails });
63
+ }
64
+ });
65
+ };
66
+
67
+ const handleRequest = async (server: McpServer, c: Context<BlankEnv, '/mcp', BlankInput>) => {
68
+ const transport = new StreamableHTTPTransport({
69
+ sessionIdGenerator: undefined, // セッションIDを生成しない(ステートレスモード)
70
+ enableJsonResponse: true,
71
+ });
72
+ try {
73
+ await server.connect(transport);
74
+ logger.trace('MCP リクエストを受信');
75
+ return await transport.handleRequest(c);
76
+ } catch (error) {
77
+ try {
78
+ await closeResources(server, transport);
79
+ } catch (closeError) {
80
+ const errorDetails = closeError instanceof Error
81
+ ? { message: closeError.message, stack: closeError.stack }
82
+ : closeError;
83
+ logger.error('Transport close failed after connection error:', { closeError: errorDetails });
84
+ }
85
+ return handleError(c, error, 'MCP 接続中のエラー:');
86
+ }
87
+
88
+ };
89
+
90
+ export const createHonoApp = (server: McpServer) => {
91
+ const app = new Hono();
92
+
93
+ app.post('/mcp', async (c) => {
94
+ return await handleRequest(server, c);
95
+ });
96
+
97
+ app.get('/mcp', async (c) => {
98
+ return await handleRequest(server, c);
99
+ });
100
+
101
+ app.put('/mcp', methodNotAllowedHandler);
102
+ app.delete('/mcp', methodNotAllowedHandler);
103
+ app.patch('/mcp', methodNotAllowedHandler);
104
+ app.options('/mcp', methodNotAllowedHandler);
105
+
106
+ return app;
107
+ };
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": [
3
+ "./tsconfig.json"
4
+ ],
5
+ "compilerOptions": {
6
+ "noEmit": true
7
+ },
8
+ "include": [
9
+ "src",
10
+ "*.config.ts"
11
+ ],
12
+ "exclude": [
13
+ "node_modules"
14
+ ]
15
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2024",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "lib": [
7
+ "es2024"
8
+ ],
9
+ "declaration": true,
10
+ "strict": true,
11
+ "noImplicitAny": true,
12
+ "strictNullChecks": true,
13
+ "noImplicitThis": true,
14
+ "alwaysStrict": true,
15
+ "noUnusedLocals": false,
16
+ "noUnusedParameters": false,
17
+ "noImplicitReturns": true,
18
+ "noFallthroughCasesInSwitch": false,
19
+ "inlineSourceMap": true,
20
+ "inlineSources": true,
21
+ "experimentalDecorators": true,
22
+ "strictPropertyInitialization": false,
23
+ "skipLibCheck": true,
24
+ "declarationMap": true,
25
+ "outDir": "dist",
26
+ "typeRoots": [
27
+ "./node_modules/@types"
28
+ ],
29
+ "types": [
30
+ "node"
31
+ ]
32
+ },
33
+ "exclude": [
34
+ "node_modules",
35
+ "*.config.ts",
36
+ "dist"
37
+ ]
38
+ }