@iflow-mcp/apple-rag-mcp 4.6.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.
Files changed (148) hide show
  1. package/.github/workflows/release.yml +62 -0
  2. package/.releaserc.json +38 -0
  3. package/CHANGELOG.md +161 -0
  4. package/README.md +114 -0
  5. package/README.zh-CN.md +119 -0
  6. package/apple-rag-mcp_process.log +8 -0
  7. package/biome.json +59 -0
  8. package/dist/src/auth/auth-middleware.d.ts +26 -0
  9. package/dist/src/auth/auth-middleware.d.ts.map +1 -0
  10. package/dist/src/auth/auth-middleware.js +77 -0
  11. package/dist/src/auth/auth-middleware.js.map +1 -0
  12. package/dist/src/auth/token-validator.d.ts +22 -0
  13. package/dist/src/auth/token-validator.d.ts.map +1 -0
  14. package/dist/src/auth/token-validator.js +64 -0
  15. package/dist/src/auth/token-validator.js.map +1 -0
  16. package/dist/src/mcp/formatters/response-formatter.d.ts +26 -0
  17. package/dist/src/mcp/formatters/response-formatter.d.ts.map +1 -0
  18. package/dist/src/mcp/formatters/response-formatter.js +119 -0
  19. package/dist/src/mcp/formatters/response-formatter.js.map +1 -0
  20. package/dist/src/mcp/manifest.d.ts +48 -0
  21. package/dist/src/mcp/manifest.d.ts.map +1 -0
  22. package/dist/src/mcp/manifest.js +46 -0
  23. package/dist/src/mcp/manifest.js.map +1 -0
  24. package/dist/src/mcp/middleware/request-validator.d.ts +48 -0
  25. package/dist/src/mcp/middleware/request-validator.d.ts.map +1 -0
  26. package/dist/src/mcp/middleware/request-validator.js +102 -0
  27. package/dist/src/mcp/middleware/request-validator.js.map +1 -0
  28. package/dist/src/mcp/protocol-handler.d.ts +70 -0
  29. package/dist/src/mcp/protocol-handler.d.ts.map +1 -0
  30. package/dist/src/mcp/protocol-handler.js +285 -0
  31. package/dist/src/mcp/protocol-handler.js.map +1 -0
  32. package/dist/src/mcp/tools/fetch-tool.d.ts +18 -0
  33. package/dist/src/mcp/tools/fetch-tool.d.ts.map +1 -0
  34. package/dist/src/mcp/tools/fetch-tool.js +76 -0
  35. package/dist/src/mcp/tools/fetch-tool.js.map +1 -0
  36. package/dist/src/mcp/tools/search-tool.d.ts +20 -0
  37. package/dist/src/mcp/tools/search-tool.d.ts.map +1 -0
  38. package/dist/src/mcp/tools/search-tool.js +86 -0
  39. package/dist/src/mcp/tools/search-tool.js.map +1 -0
  40. package/dist/src/services/database.d.ts +37 -0
  41. package/dist/src/services/database.d.ts.map +1 -0
  42. package/dist/src/services/database.js +166 -0
  43. package/dist/src/services/database.js.map +1 -0
  44. package/dist/src/services/deepinfra-base.d.ts +22 -0
  45. package/dist/src/services/deepinfra-base.d.ts.map +1 -0
  46. package/dist/src/services/deepinfra-base.js +55 -0
  47. package/dist/src/services/deepinfra-base.js.map +1 -0
  48. package/dist/src/services/embedding.d.ts +44 -0
  49. package/dist/src/services/embedding.d.ts.map +1 -0
  50. package/dist/src/services/embedding.js +61 -0
  51. package/dist/src/services/embedding.js.map +1 -0
  52. package/dist/src/services/index.d.ts +10 -0
  53. package/dist/src/services/index.d.ts.map +1 -0
  54. package/dist/src/services/index.js +52 -0
  55. package/dist/src/services/index.js.map +1 -0
  56. package/dist/src/services/ip-authentication.d.ts +12 -0
  57. package/dist/src/services/ip-authentication.d.ts.map +1 -0
  58. package/dist/src/services/ip-authentication.js +39 -0
  59. package/dist/src/services/ip-authentication.js.map +1 -0
  60. package/dist/src/services/rag.d.ts +35 -0
  61. package/dist/src/services/rag.d.ts.map +1 -0
  62. package/dist/src/services/rag.js +106 -0
  63. package/dist/src/services/rag.js.map +1 -0
  64. package/dist/src/services/rate-limit.d.ts +27 -0
  65. package/dist/src/services/rate-limit.d.ts.map +1 -0
  66. package/dist/src/services/rate-limit.js +91 -0
  67. package/dist/src/services/rate-limit.js.map +1 -0
  68. package/dist/src/services/reranker.d.ts +40 -0
  69. package/dist/src/services/reranker.d.ts.map +1 -0
  70. package/dist/src/services/reranker.js +97 -0
  71. package/dist/src/services/reranker.js.map +1 -0
  72. package/dist/src/services/search-engine.d.ts +89 -0
  73. package/dist/src/services/search-engine.d.ts.map +1 -0
  74. package/dist/src/services/search-engine.js +225 -0
  75. package/dist/src/services/search-engine.js.map +1 -0
  76. package/dist/src/services/tool-call-logger.d.ts +36 -0
  77. package/dist/src/services/tool-call-logger.d.ts.map +1 -0
  78. package/dist/src/services/tool-call-logger.js +34 -0
  79. package/dist/src/services/tool-call-logger.js.map +1 -0
  80. package/dist/src/types/env.d.ts +18 -0
  81. package/dist/src/types/env.d.ts.map +1 -0
  82. package/dist/src/types/env.js +2 -0
  83. package/dist/src/types/env.js.map +1 -0
  84. package/dist/src/types/index.d.ts +145 -0
  85. package/dist/src/types/index.d.ts.map +1 -0
  86. package/dist/src/types/index.js +6 -0
  87. package/dist/src/types/index.js.map +1 -0
  88. package/dist/src/utils/d1-utils.d.ts +6 -0
  89. package/dist/src/utils/d1-utils.d.ts.map +1 -0
  90. package/dist/src/utils/d1-utils.js +29 -0
  91. package/dist/src/utils/d1-utils.js.map +1 -0
  92. package/dist/src/utils/logger.d.ts +11 -0
  93. package/dist/src/utils/logger.d.ts.map +1 -0
  94. package/dist/src/utils/logger.js +26 -0
  95. package/dist/src/utils/logger.js.map +1 -0
  96. package/dist/src/utils/query-cleaner.d.ts +20 -0
  97. package/dist/src/utils/query-cleaner.d.ts.map +1 -0
  98. package/dist/src/utils/query-cleaner.js +117 -0
  99. package/dist/src/utils/query-cleaner.js.map +1 -0
  100. package/dist/src/utils/request-info.d.ts +18 -0
  101. package/dist/src/utils/request-info.d.ts.map +1 -0
  102. package/dist/src/utils/request-info.js +32 -0
  103. package/dist/src/utils/request-info.js.map +1 -0
  104. package/dist/src/utils/telegram-notifier.d.ts +4 -0
  105. package/dist/src/utils/telegram-notifier.d.ts.map +1 -0
  106. package/dist/src/utils/telegram-notifier.js +33 -0
  107. package/dist/src/utils/telegram-notifier.js.map +1 -0
  108. package/dist/src/utils/url-processor.d.ts +15 -0
  109. package/dist/src/utils/url-processor.d.ts.map +1 -0
  110. package/dist/src/utils/url-processor.js +54 -0
  111. package/dist/src/utils/url-processor.js.map +1 -0
  112. package/dist/src/worker.d.ts +15 -0
  113. package/dist/src/worker.d.ts.map +1 -0
  114. package/dist/src/worker.js +136 -0
  115. package/dist/src/worker.js.map +1 -0
  116. package/migrations/schema.sql +155 -0
  117. package/package.json +49 -0
  118. package/scripts/semantic-release-server-json.js +34 -0
  119. package/server.json +25 -0
  120. package/src/auth/auth-middleware.ts +104 -0
  121. package/src/auth/token-validator.ts +96 -0
  122. package/src/mcp/formatters/response-formatter.ts +157 -0
  123. package/src/mcp/manifest.ts +48 -0
  124. package/src/mcp/middleware/request-validator.ts +135 -0
  125. package/src/mcp/protocol-handler.ts +412 -0
  126. package/src/mcp/tools/fetch-tool.ts +146 -0
  127. package/src/mcp/tools/search-tool.ts +165 -0
  128. package/src/services/database.ts +202 -0
  129. package/src/services/deepinfra-base.ts +81 -0
  130. package/src/services/embedding.ts +96 -0
  131. package/src/services/index.ts +59 -0
  132. package/src/services/ip-authentication.ts +62 -0
  133. package/src/services/rag.ts +158 -0
  134. package/src/services/rate-limit.ts +141 -0
  135. package/src/services/reranker.ts +171 -0
  136. package/src/services/search-engine.ts +333 -0
  137. package/src/services/tool-call-logger.ts +98 -0
  138. package/src/types/env.ts +22 -0
  139. package/src/types/index.ts +189 -0
  140. package/src/utils/d1-utils.ts +45 -0
  141. package/src/utils/logger.ts +33 -0
  142. package/src/utils/query-cleaner.ts +151 -0
  143. package/src/utils/request-info.ts +47 -0
  144. package/src/utils/telegram-notifier.ts +47 -0
  145. package/src/utils/url-processor.ts +65 -0
  146. package/src/worker.ts +176 -0
  147. package/tsconfig.json +32 -0
  148. package/wrangler.toml.example +39 -0
package/src/worker.ts ADDED
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Apple RAG MCP Server - Cloudflare Worker Native
3
+ * Ultra-modern, zero-dependency MCP 2025-06-18 compliant server
4
+ * Global optimal solution with maximum performance
5
+ */
6
+
7
+ import { HEALTH_STATUS, SERVER_MANIFEST } from "./mcp/manifest.js";
8
+ import { MCPProtocolHandler } from "./mcp/protocol-handler.js";
9
+ import { createServices } from "./services/index.js";
10
+ import type { WorkerEnv } from "./types/index.js";
11
+ import { logger } from "./utils/logger.js";
12
+ import { configureTelegram } from "./utils/telegram-notifier.js";
13
+
14
+ /**
15
+ * Cloudflare Worker entry point - Global optimal implementation
16
+ * Handles all MCP protocol requests with edge-optimized performance
17
+ */
18
+ export default {
19
+ async fetch(
20
+ request: Request,
21
+ env: WorkerEnv,
22
+ ctx: ExecutionContext
23
+ ): Promise<Response> {
24
+ const startTime = performance.now();
25
+
26
+ // Configure Telegram notification and set execution context for waitUntil
27
+ configureTelegram(env.TELEGRAM_BOT_URL);
28
+ logger.setContext(ctx);
29
+
30
+ try {
31
+ const url = new URL(request.url);
32
+
33
+ // Health check endpoint - ultra-fast response
34
+ if (request.method === "GET" && url.pathname === "/health") {
35
+ return new Response(
36
+ JSON.stringify({
37
+ ...HEALTH_STATUS,
38
+ timestamp: new Date().toISOString(),
39
+ }),
40
+ {
41
+ status: 200,
42
+ headers: {
43
+ "Content-Type": "application/json",
44
+ "Cache-Control": "no-cache",
45
+ },
46
+ }
47
+ );
48
+ }
49
+
50
+ // Manifest endpoint - server discovery
51
+ if (request.method === "GET" && url.pathname === "/manifest") {
52
+ return new Response(JSON.stringify(SERVER_MANIFEST), {
53
+ status: 200,
54
+ headers: {
55
+ "Content-Type": "application/json",
56
+ "Cache-Control": "public, max-age=3600",
57
+ },
58
+ });
59
+ }
60
+
61
+ // Reject other GET requests to the MCP endpoint
62
+ if (request.method === "GET" && url.pathname === "/") {
63
+ return new Response("Method Not Allowed", {
64
+ status: 405,
65
+ headers: {
66
+ "Access-Control-Allow-Origin": "*",
67
+ Allow: "POST, OPTIONS",
68
+ },
69
+ });
70
+ }
71
+
72
+ // Handle POST /manifest requests (VPS compatibility)
73
+ if (request.method === "POST" && url.pathname === "/manifest") {
74
+ try {
75
+ const body = await request.json();
76
+
77
+ // Empty body → return manifest (common client behavior)
78
+ if (!body || Object.keys(body).length === 0) {
79
+ return new Response(JSON.stringify(SERVER_MANIFEST), {
80
+ status: 200,
81
+ headers: { "Content-Type": "application/json" },
82
+ });
83
+ }
84
+
85
+ // MCP request to wrong endpoint → redirect to correct endpoint
86
+ if (
87
+ (body as unknown as { jsonrpc?: string; method?: string })
88
+ .jsonrpc === "2.0" &&
89
+ (body as unknown as { jsonrpc?: string; method?: string }).method
90
+ ) {
91
+ return new Response(
92
+ JSON.stringify({
93
+ error: "Endpoint redirect",
94
+ message: "MCP protocol requests should be sent to /",
95
+ redirect: "/",
96
+ }),
97
+ {
98
+ status: 307,
99
+ headers: {
100
+ "Content-Type": "application/json",
101
+ Location: "/",
102
+ },
103
+ }
104
+ );
105
+ }
106
+
107
+ // Any other POST data → helpful error
108
+ return new Response(
109
+ JSON.stringify({
110
+ error: "Invalid manifest request",
111
+ message:
112
+ "Use GET /manifest for server discovery or POST / for MCP communication",
113
+ endpoints: {
114
+ manifest: "GET /manifest",
115
+ mcp: "POST /",
116
+ },
117
+ }),
118
+ {
119
+ status: 400,
120
+ headers: { "Content-Type": "application/json" },
121
+ }
122
+ );
123
+ } catch (_error) {
124
+ return new Response(
125
+ JSON.stringify({
126
+ error: "Invalid JSON",
127
+ message: "Request body must be valid JSON",
128
+ }),
129
+ {
130
+ status: 400,
131
+ headers: { "Content-Type": "application/json" },
132
+ }
133
+ );
134
+ }
135
+ }
136
+
137
+ // Initialize services with Worker environment
138
+ const services = await createServices(env);
139
+
140
+ // Authenticate request using auth service
141
+ const authContext = await services.auth.optionalAuth(request);
142
+
143
+ // Create MCP protocol handler
144
+ const handler = new MCPProtocolHandler(services);
145
+
146
+ // Handle MCP request
147
+ const response = await handler.handleRequest(request, authContext);
148
+
149
+ return response;
150
+ } catch (error) {
151
+ const duration = performance.now() - startTime;
152
+ const errorUrl = new URL(request.url);
153
+
154
+ logger.error(
155
+ `Worker error for ${request.method} ${errorUrl.pathname} (duration: ${Math.round(duration)}ms): ${error instanceof Error ? error.message : String(error)}`
156
+ );
157
+
158
+ return new Response(
159
+ JSON.stringify({
160
+ jsonrpc: "2.0",
161
+ error: {
162
+ code: -32603,
163
+ message: "Internal server error",
164
+ },
165
+ }),
166
+ {
167
+ status: 500,
168
+ headers: {
169
+ "Content-Type": "application/json",
170
+ "Cache-Control": "no-cache",
171
+ },
172
+ }
173
+ );
174
+ }
175
+ },
176
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["ES2022"],
5
+ "module": "ESNext",
6
+ "moduleResolution": "Node",
7
+ "skipLibCheck": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "esModuleInterop": true,
10
+ "resolveJsonModule": true,
11
+ "isolatedModules": true,
12
+
13
+ "outDir": "./dist",
14
+ "rootDir": "./",
15
+ "declaration": true,
16
+ "declarationMap": true,
17
+ "sourceMap": true,
18
+
19
+ "strict": true,
20
+ "noUnusedLocals": true,
21
+ "noUnusedParameters": true,
22
+ "noFallthroughCasesInSwitch": true,
23
+ "noImplicitReturns": true,
24
+ "noImplicitOverride": true,
25
+
26
+ "forceConsistentCasingInFileNames": true,
27
+
28
+ "types": ["node", "@cloudflare/workers-types"]
29
+ },
30
+ "include": ["src/**/*"],
31
+ "exclude": ["node_modules", "dist", "logs", "**/*.test.ts"]
32
+ }
@@ -0,0 +1,39 @@
1
+ name = "apple-rag-mcp"
2
+ main = "src/worker.ts"
3
+ compatibility_date = "2024-12-01"
4
+ compatibility_flags = ["nodejs_compat"]
5
+
6
+ # Cloudflare Workers Logs Configuration
7
+ [observability]
8
+ enabled = true
9
+ head_sampling_rate = 1
10
+
11
+ # Smart Placement Configuration
12
+ # Automatically places Worker closer to your database for better performance
13
+ [placement]
14
+ mode = "smart"
15
+
16
+ # Shared configuration for all environments
17
+ # Replace with your actual values
18
+ [vars]
19
+ RAG_DB_HOST = "your_postgresql_host"
20
+ RAG_DB_PORT = "5432"
21
+ RAG_DB_DATABASE = "your_database_name"
22
+ RAG_DB_USER = "your_database_user"
23
+ RAG_DB_PASSWORD = "your_database_password"
24
+ RAG_DB_SSLMODE = "disable"
25
+ TELEGRAM_BOT_URL = "your_telegram_bot_url"
26
+
27
+ # Development environment D1 database
28
+ # Replace with your own D1 database configuration
29
+ [[d1_databases]]
30
+ binding = "DB"
31
+ database_name = "your_d1_database_name"
32
+ database_id = "your_d1_database_id"
33
+
34
+ # Production environment D1 database
35
+ # Replace with your own production D1 database configuration
36
+ [[env.production.d1_databases]]
37
+ binding = "DB"
38
+ database_name = "your_production_d1_database_name"
39
+ database_id = "your_production_d1_database_id"