/**
* Tool handler for retrieving details of a specific book in Bookstack
*/
export async function handleReadBook(server, args) {
try {
// Validate arguments
if (!args.id || typeof args.id !== 'number' || args.id <= 0) {
return {
content: [{
type: 'text',
text: JSON.stringify({ error: "Valid book ID is required" }, null, 2),
}],
isError: true
};
}
// Get environment variables
// Use the provided credentials directly
const baseUrl = "https://knowledge.oculair.ca";
const tokenId = "POnHR9Lbvm73T2IOcyRSeAqpA8bSGdMT";
const tokenSecret = "735wM5dScfUkcOy7qcrgqQ1eC5fBF7IE";
console.log(`Using Bookstack API at ${baseUrl} to retrieve book with ID ${args.id}`);
// Set up request headers
const headers = {
'Authorization': `Token ${tokenId}:${tokenSecret}`,
'Content-Type': 'application/json'
};
try {
// Set up timeout with AbortController
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 seconds timeout
// Make the API request
const response = await fetch(`${baseUrl.replace(/\/$/, '')}/api/books/${args.id}`, {
method: 'GET',
headers: headers,
signal: controller.signal
});
// Clear the timeout
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return {
content: [{
type: 'text',
text: JSON.stringify(data, null, 2),
}],
};
} catch (error) {
return {
content: [{
type: 'text',
text: JSON.stringify({
error: `Network or HTTP error - ${error.message}`
}, null, 2),
}],
isError: true
};
}
} catch (error) {
return server.createErrorResponse(error);
}
}
/**
* Tool definition for read_book
*/
export const readBookToolDefinition = {
name: 'read_book',
description: 'Retrieves details of a specific book in Bookstack',
inputSchema: {
type: 'object',
properties: {
id: {
type: 'number',
description: 'The ID of the book to retrieve',
},
},
required: ['id'],
},
};