create-note.ts•2.7 kB
import { BaseMCPTool } from '../mcp/registry/base-tool.js';
import { ReadwiseAPI } from '../api/readwise-api.js';
import { CreateNoteParams, Highlight, MCPToolResult, isAPIError } from '../types/index.js';
import { ValidationResult, validateRequired } from '../types/validation.js';
import type { Logger } from '../utils/logger-interface.js';
/**
* Tool for creating notes on highlights
*/
export class CreateNoteTool extends BaseMCPTool<CreateNoteParams, Highlight> {
/**
* The name of the tool
*/
readonly name = 'create_note';
/**
* The description of the tool
*/
readonly description = 'Create a note on a highlight in your Readwise library';
/**
* The JSON Schema parameters for the tool
*/
readonly parameters = {
type: 'object',
properties: {
highlight_id: {
type: 'string',
description: 'The ID of the highlight to add a note to'
},
note: {
type: 'string',
description: 'The note text to add to the highlight'
}
},
required: ['highlight_id', 'note']
};
/**
* Create a new CreateNoteTool
*/
constructor(private api: ReadwiseAPI, logger: Logger) {
super(logger);
}
/**
* Validate the parameters
*/
validate(params: CreateNoteParams): ValidationResult {
const validations = [
validateRequired(params, 'highlight_id', 'Highlight ID is required'),
validateRequired(params, 'note', 'Note text is required')
];
// Check each validation result
for (const validation of validations) {
if (!validation.valid) {
return validation;
}
}
// All validations passed
return super.validate(params);
}
/**
* Execute the tool
*/
async execute(params: CreateNoteParams): Promise<MCPToolResult<Highlight>> {
try {
this.logger.debug('Executing create_note tool', params as any);
const highlight = await this.api.createNote(params);
this.logger.debug(`Created note on highlight ${params.highlight_id}`);
return { result: highlight };
} catch (error) {
this.logger.error('Error executing create_note tool', error as any);
// Re-throw API errors
if (isAPIError(error)) {
throw error;
}
// Handle unexpected errors with proper result format
return {
result: {
id: params.highlight_id,
text: '',
book_id: '',
note: params.note,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
},
success: false,
error: 'An unexpected error occurred while creating the note'
};
}
}
}