Skip to content

Commit 6baff5c

Browse files
committed
style(core): optimize imports and remove deprecated types
- Update imports in handlers and tools to reference context.js directly - Remove deprecated HandlerContext alias from index.ts - Remove ResolvedContext re-export from index.ts to prevent circular dependencies - Update create-feature.sh template to use correct import path
1 parent 7843308 commit 6baff5c

13 files changed

Lines changed: 81 additions & 58 deletions

File tree

CONTRIBUTING.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ src/
118118
**模块说明**
119119
- **app**: 基础应用管理(开发者/应用选择、OAuth 授权、环境检查)
120120
- **leaderboard**: 排行榜功能(依赖 app 模块)
121+
- **h5Game**: H5 游戏管理
122+
- **vibrate**: 振动 API 文档
121123
- 未来: cloudSave, share 等(都可以复用 app 模块)
122124

123125
---
@@ -216,7 +218,7 @@ export const myTools: ToolRegistration[] = [
216218
description: '...',
217219
inputSchema: { ... }
218220
},
219-
handler: async (args: { param: string }, context) => {
221+
handler: async (args: { param: string }, context: ResolvedContext) => {
220222
// 实现逻辑
221223
}
222224
}
@@ -357,9 +359,11 @@ TapTap API
357359
|------|-------|---------|------|
358360
| app | 4 | ~430 行 | 应用管理基础功能 |
359361
| leaderboard | 7 | ~1350 行 | 排行榜(已分离 app 操作)|
360-
| core | 9 | ~900 行 | 共享核心代码 |
361-
| server.ts | 1 | ~300 行 | 主服务器 |
362-
| **总计** | **21** | **~2980 行** | |
362+
| h5Game | 5 | ~600 行 | H5 游戏管理 |
363+
| vibrate | 6 | ~300 行 | 振动 API 文档 |
364+
| core | 10 | ~1100 行 | 共享核心代码 |
365+
| server.ts | 1 | ~450 行 | 主服务器 |
366+
| **总计** | **33** | **~4230 行** | |
363367
364368
**架构优化成果**
365369
- ✅ 模块化后清理重复代码

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> 基于 Model Context Protocol (MCP) 的 **TapTap 小游戏和 H5 游戏**服务器 - 提供排行榜文档和管理 API,支持 **OAuth 2.0 零配置认证**
44
5-
🔐 **零配置 OAuth** | 📚 **完整文档** | 🎯 **19 Tools + 7 Resources** | 🌍 **小游戏 & H5** | 📦 **单文件 Bundle**
5+
🔐 **零配置 OAuth** | 📚 **完整文档** | 🎯 **19 Tools + 11 Resources** | 🌍 **小游戏 & H5** | 📦 **单文件 Bundle**
66

77
## ✨ 核心特性
88

@@ -128,7 +128,7 @@ curl http://localhost:5003/health
128128
#### 振动 API 文档 (1)
129129
- `get_vibrate_integration_guide` - 振动 API 完整文档和接入指引
130130

131-
### 7 个 Resources
131+
### 11 个 Resources
132132

133133
完整的排行榜 API 文档:
134134
- `docs://leaderboard/overview` - 完整概览
@@ -139,6 +139,12 @@ curl http://localhost:5003/health
139139
- `docs://leaderboard/api/load-player-score` - 玩家排名
140140
- `docs://leaderboard/api/load-centered-scores` - 周围玩家
141141

142+
完整的振动 API 文档:
143+
- `docs://vibrate/overview` - 完整概览
144+
- `docs://vibrate/api/vibrate-short` - 短振动 API
145+
- `docs://vibrate/api/vibrate-long` - 长振动 API
146+
- `docs://vibrate/patterns` - 使用模式和最佳实践
147+
142148
## 🎯 使用示例
143149

144150
### 接入排行榜

docs/ARCHITECTURE.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ src/
4040
│ │ ├── handlers.ts # 业务逻辑
4141
│ │ └── api.ts # API 调用
4242
│ │
43+
│ ├── h5Game/ # H5 游戏模块
44+
│ │ ├── index.ts # 模块定义
45+
│ │ ├── tools.ts # 工具定义
46+
│ │ ├── handlers.ts # 业务逻辑
47+
│ │ ├── api.ts # API 调用
48+
│ │ └── messages.ts # 消息常量
49+
│ │
50+
│ ├── vibrate/ # 振动 API 文档模块
51+
│ │ ├── index.ts # 模块定义
52+
│ │ ├── tools.ts # 工具定义
53+
│ │ ├── resources.ts # 资源定义
54+
│ │ ├── docs.ts # 文档内容
55+
│ │ └── docTools.ts # 文档工具
56+
│ │
4357
│ └── 未来功能/ # cloudSave/, share/ 等
4458
4559
├── core/ # 跨模块共享代码
@@ -72,6 +86,16 @@ bin/
7286
- 排行榜 API 文档 Resources
7387
- 用户分数查询
7488

89+
- **h5Game 模块** - H5 游戏管理
90+
- H5 游戏信息收集
91+
- 游戏包上传和发布
92+
- 游戏创建和状态查询
93+
94+
- **vibrate 模块** - 振动 API 文档
95+
- 振动功能接入指引
96+
- 完整的 API 文档 Resources
97+
- 最佳实践和使用模式
98+
7599
- **未来模块** - cloudSave(云存档)、share(分享)等
76100

77101
#### 核心共享层(`src/core/`
@@ -328,35 +352,42 @@ export interface ResourceRegistration {
328352

329353
### 实现方案
330354

331-
服务器层是唯一处理私有参数的地方:
355+
服务器层是唯一处理私有参数的地方,使用 `ResolvedContext` 进行统一封装
332356

333357
```typescript
334358
// Server 层(src/server.ts)
335359
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
336-
const toolName = request.params.name;
337-
const args = request.params.arguments || {};
360+
const { name, arguments: args } = request.params;
338361

339-
// 1. 提取私有参数(从 arguments 或 HTTP Header)
340-
const enrichedArgs = injectPrivateParams(args, extra);
362+
// 1. 从 HTTP Headers 提取私有参数(仅 HTTP/SSE 模式)
363+
let enrichedArgs = args || {};
364+
if (extra?.requestInfo?.headers) {
365+
enrichedArgs = extractPrivateParamsFromHeaders(enrichedArgs, extra.requestInfo.headers);
366+
}
341367

342-
// 2. 合并到 context
343-
const effectiveContext = getEffectiveContext(enrichedArgs, baseContext);
368+
// 2. 构建 ResolvedContext(合并 args + baseContext)
369+
// baseContext 来自 Session 闭包(userId, projectId)
370+
const ctx = new ResolvedContext(enrichedArgs, baseContext);
344371

345372
// 3. 移除私有参数,业务层不可见
346373
const businessArgs = stripPrivateParams(enrichedArgs);
347374

348-
// 4. 调用业务层
349-
const result = await toolReg.handler(businessArgs, effectiveContext);
375+
// 4. 调用业务层(传入 context)
376+
const result = await toolReg.handler(businessArgs, ctx);
350377

351378
return result;
352379
});
353380
```
354381

355-
业务层代码完全不感知私有参数:
382+
业务层代码完全不感知私有参数,通过 Context 访问
356383

357384
```typescript
358385
// 业务层(features/leaderboard/handlers.ts)
359-
handler: async (args: { page: number }, context) => {
386+
handler: async (args: { page: number }, context: ResolvedContext) => {
387+
// 通过 context 获取认证和应用信息
388+
const token = context.resolveToken();
389+
const app = context.resolveApp();
390+
360391
// 简洁的业务逻辑
361392
return api.listLeaderboards(args, context);
362393
}
@@ -586,10 +617,11 @@ const overview = generateOverview(documentation);
586617
|------|-------|---------|------|
587618
| **app** | 4 | ~430 行 | 应用管理基础功能 |
588619
| **leaderboard** | 7 | ~1350 行 | 排行榜(已分离 app 操作)|
589-
| **h5game** | 4 | ~600 行 | H5 游戏管理 |
620+
| **h5Game** | 5 | ~600 行 | H5 游戏管理 |
621+
| **vibrate** | 6 | ~300 行 | 振动 API 文档 |
590622
| **core** | 10 | ~1100 行 | 共享核心代码 |
591623
| **server.ts** | 1 | ~450 行 | 主服务器(支持 SSE/HTTP)|
592-
| **总计** | **26** | **~3930** | |
624+
| **总计** | **33** | **~4230** | |
593625

594626
### 架构优化成果
595627

docs/PROXY.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,6 @@ echo -n '{"kid":"abc123","mac_key":"secret","token_type":"mac","mac_algorithm":"
226226
}
227227
```
228228

229-
#### Server 层统一处理
230-
231-
```typescript
232-
// Server 层(唯一处理私有参数的地方)
233-
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
234-
const { arguments: args } = request.params;
235-
236-
// 1. 提取私有参数(从 arguments 或 HTTP Header)
237-
const effectiveContext = getEffectiveContext(enrichedArgs, baseContext);
238-
239-
// 2. 移除私有参数
240-
const businessArgs = stripPrivateParams(enrichedArgs);
241-
242-
// 3. 调用业务层(完全干净)
243-
await toolReg.handler(businessArgs, effectiveContext);
244-
});
245-
```
246229

247230
### 3.7 工作流程
248231

scripts/create-feature.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ cat > "$FEATURE_DIR/index.ts" << EOF
180180
*/
181181
182182
import { Tool } from '@modelcontextprotocol/sdk/types.js';
183-
import type { HandlerContext } from '../../core/types/index.js';
183+
import type { ResolvedContext } from '../../core/types/index.js';
184184
185185
import { ${CAMEL_CASE}ToolDefinitions, ${CAMEL_CASE}ToolHandlers } from './tools.js';
186186
EOF
@@ -248,7 +248,7 @@ cat > "$FEATURE_DIR/tools.ts" << EOF
248248
*/
249249
250250
import { Tool } from '@modelcontextprotocol/sdk/types.js';
251-
import type { HandlerContext } from '../../core/types/index.js';
251+
import type { ResolvedContext } from '../../core/types/context.js';
252252
253253
import * as handlers from './handlers.js';
254254
import { ${CAMEL_CASE}Tools } from './docTools.js';
@@ -315,21 +315,21 @@ export const ${CAMEL_CASE}ToolDefinitions: Tool[] = [
315315
*/
316316
export const ${CAMEL_CASE}ToolHandlers = [
317317
// get_${FEATURE_KEY}_integration_guide
318-
async (args: any, context: HandlerContext) => {
318+
async (args: any, context: ResolvedContext) => {
319319
return ${CAMEL_CASE}Tools.getIntegrationWorkflow();
320320
},
321321
322322
// TODO: 添加更多 handlers
323323
// 示例 - save_${FEATURE_KEY}_data handler:
324324
/*
325-
async (args: { key: string; value: string }, context: HandlerContext) => {
325+
async (args: { key: string; value: string }, context: ResolvedContext) => {
326326
return handlers.saveData(args, context);
327327
},
328328
*/
329329
330330
// 示例 - load_${FEATURE_KEY}_data handler:
331331
/*
332-
async (args: { key: string }, context: HandlerContext) => {
332+
async (args: { key: string }, context: ResolvedContext) => {
333333
return handlers.loadData(args, context);
334334
}
335335
*/
@@ -698,15 +698,15 @@ cat > "$FEATURE_DIR/handlers.ts" << EOF
698698
* $FEATURE_NAME Handlers
699699
*/
700700
701-
import type { HandlerContext } from '../../core/types/index.js';
701+
import type { ResolvedContext } from '../../core/types/index.js';
702702
import * as api from './api.js';
703703
704704
// TODO: 实现业务逻辑处理器
705705
// 示例:
706706
/*
707707
export async function saveData(
708708
args: { key: string; value: string },
709-
context: HandlerContext
709+
context: ResolvedContext
710710
): Promise<string> {
711711
const { key, value } = args;
712712
@@ -720,7 +720,7 @@ Value: \${value}\`;
720720
721721
export async function loadData(
722722
args: { key: string },
723-
context: HandlerContext
723+
context: ResolvedContext
724724
): Promise<string> {
725725
const { key } = args;
726726
@@ -745,7 +745,7 @@ cat > "$FEATURE_DIR/api.ts" << EOF
745745
746746
import { HttpClient } from '../../core/network/httpClient.js';
747747
import { ensureAppInfo } from '../app/api.js'; // 导入应用信息函数
748-
import type { HandlerContext } from '../../core/types/index.js';
748+
import type { ResolvedContext } from '../../core/types/context.js';
749749
750750
// TODO: 定义接口
751751
// 示例:
@@ -777,11 +777,12 @@ export interface LoadDataResponse {
777777
/*
778778
export async function saveDataToCloud(
779779
args: { key: string; value: string },
780-
context: HandlerContext
780+
context: ResolvedContext
781781
): Promise<SaveDataResponse> {
782782
const client = new HttpClient();
783783
784784
// 获取应用信息(developer_id, app_id 等)
785+
// 注意:ensureAppInfo 现在接受 projectPath 字符串,从 context.projectPath 获取
785786
const appInfo = await ensureAppInfo(context.projectPath);
786787
787788
const response = await client.post<SaveDataResponse>(
@@ -801,7 +802,7 @@ export async function saveDataToCloud(
801802
802803
export async function loadDataFromCloud(
803804
args: { key: string },
804-
context: HandlerContext
805+
context: ResolvedContext
805806
): Promise<string> {
806807
const client = new HttpClient();
807808

src/core/types/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ export type {
3131
SessionContext,
3232
TokenSource
3333
} from './context.js';
34-
export { ResolvedContext, getTokenSourceLabel } from './context.js';
35-
36-
// 向后兼容别名(将逐步废弃)
37-
export type { RequestContext as HandlerContext } from './context.js';
34+
export { getTokenSourceLabel } from './context.js';
3835

3936
// 前向声明
4037
import type { ResolvedContext as RC } from './context.js';

src/features/app/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Handles developer and app selection operations
44
*/
55

6-
import type { ResolvedContext } from '../../core/types/index.js';
6+
import type { ResolvedContext } from '../../core/types/context.js';
77
import { getAllDevelopersAndApps, selectApp as selectAppApi } from './api.js';
88
import { clearAppCache } from '../../core/utils/cache.js';
99
import { clearToken, saveToken } from '../../core/auth/tokenStorage.js';

src/features/app/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Unified definitions and handlers for app operations
44
*/
55

6-
import type { ToolRegistration, HandlerContext } from '../../core/types/index.js';
6+
import type { ToolRegistration } from '../../core/types/index.js';
77
import * as appHandlers from './handlers.js';
88
import * as appApi from './api.js';
99

src/features/h5Game/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { readAppCache, saveAppCache, type AppCacheInfo } from '../../core/utils/
2323
import { logger } from '../../core/utils/logger.js';
2424
import { resolveWorkPath } from '../../core/utils/pathResolver.js';
2525
import { EnvConfig } from '../../core/utils/env.js';
26-
import type { ResolvedContext } from '../../core/types/index.js';
26+
import type { ResolvedContext } from '../../core/types/context.js';
2727

2828
/**
2929
* 临时文件根目录(独立于 workspace)

src/features/leaderboard/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Handles leaderboard operations including creation, listing, and workflow guidance
44
*/
55

6-
import type { ResolvedContext } from '../../core/types/index.js';
6+
import type { ResolvedContext } from '../../core/types/context.js';
77
import {
88
createLeaderboard as createLeaderboardApi,
99
listLeaderboards as listLeaderboardsApi,

0 commit comments

Comments
 (0)