createDocument
Add or update documents in a CouchDB database by specifying the database name, document ID, and data. Simplifies database interactions for AI assistants using TypeScript-based MCP servers.
Instructions
Create a new document or update an existing document in a database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | Document data | |
| dbName | Yes | Database name | |
| docId | Yes | Document ID |
Implementation Reference
- src/index.ts:337-369 (handler)The handler function that validates input parameters, retrieves the database instance, inserts the document using nano's insert method (handles create/update), and returns success or error message.private async handleCreateDocument(args: any) { if (!args.dbName || !args.docId || !args.data) { throw new McpError( ErrorCode.InvalidParams, 'Missing required parameters: dbName, docId, data' ); } try { const db = await getDatabase(args.dbName); const response = await db.insert(args.data, args.docId); const action = args.data._rev ? 'updated' : 'created'; return { content: [ { type: 'text', text: `Document ${action} with ID: ${response.id}, rev: ${response.rev}`, }, ], }; } catch (error: any) { const action = args.data._rev ? 'updating' : 'creating'; return { content: [ { type: 'text', text: `Error ${action} document: ${error.message}`, }, ], isError: true, }; } }
- src/index.ts:90-111 (schema)The input schema defining the parameters for the createDocument tool: dbName, docId, and data object.{ name: 'createDocument', description: 'Create a new document or update an existing document in a database', inputSchema: { type: 'object', properties: { dbName: { type: 'string', description: 'Database name', }, docId: { type: 'string', description: 'Document ID', }, data: { type: 'object', description: 'Document data', }, }, required: ['dbName', 'docId', 'data'], }, },
- src/index.ts:236-237 (registration)The switch case in the CallToolRequestSchema handler that routes createDocument calls to the handler method.case 'createDocument': return this.handleCreateDocument(request.params.arguments);