position_update
Update stock position records by modifying quantity or cost price for accurate portfolio tracking in the Stock MCP Server.
Instructions
更新持仓记录(数量或成本价)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 股票代码 | |
| market | Yes | 市场 | |
| quantity | No | 持有数量 | |
| costPrice | No | 成本价 |
Implementation Reference
- src/position.ts:54-75 (handler)The updatePosition function that performs the actual position update logic - loads positions from file, finds the matching position by code and market, updates the quantity and/or costPrice, sets the updatedAt timestamp, saves back to file, and returns the updated position or null if not found.// 更新持仓 export function updatePosition( code: string, market: Market, updates: Partial<Pick<Position, 'quantity' | 'costPrice'>> ): Position | null { const positions = loadPositions(); const index = positions.findIndex(p => p.code === code && p.market === market); if (index === -1) { return null; } positions[index] = { ...positions[index], ...updates, updatedAt: new Date().toISOString(), }; savePositions(positions); return positions[index]; }
- src/index.ts:35-40 (schema)UpdatePositionSchema defines the input validation schema for position_update tool - requires code and market, with optional quantity and costPrice fields.const UpdatePositionSchema = z.object({ code: z.string().describe('股票代码'), market: z.enum(['sh', 'sz', 'hk', 'us']).describe('市场'), quantity: z.number().positive().optional().describe('持有数量'), costPrice: z.number().positive().optional().describe('成本价'), });
- src/index.ts:324-345 (handler)The MCP tool handler for position_update - validates input with UpdatePositionSchema, calls position.updatePosition with the parsed parameters, and returns the updated position or throws an error if not found.if (name === 'position_update') { const params = UpdatePositionSchema.parse(args); const result = position.updatePosition( params.code, params.market as Market, { quantity: params.quantity, costPrice: params.costPrice, } ); if (!result) { throw new Error('Position not found'); } return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/index.ts:158-171 (registration)Tool registration for position_update in the MCP server's ListToolsRequestSchema handler - defines the tool name, description, and input schema.{ name: 'position_update', description: '更新持仓记录(数量或成本价)', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '股票代码' }, market: { type: 'string', enum: ['sh', 'sz', 'hk', 'us'], description: '市场' }, quantity: { type: 'number', description: '持有数量' }, costPrice: { type: 'number', description: '成本价' }, }, required: ['code', 'market'], }, },
- src/types.ts:81-90 (schema)Position interface defining the structure of position data stored in positions.json - includes code, name, quantity, costPrice, currency, market, createdAt, and updatedAt fields.export interface Position { code: string; name: string; quantity: number; costPrice: number; currency: string; market: Market; createdAt: string; updatedAt: string; }