MCPcopy Index your code
hub / github.com/deepnote/deepnote / handleAddBlock

Function handleAddBlock

packages/mcp/src/tools/writing.ts:501–607  ·  view source on GitHub ↗
(args: Record<string, unknown>)

Source from the content-addressed store, hash-verified

499}
500
501async function handleAddBlock(args: Record<string, unknown>) {
502 const parsedArgs = addBlockArgsSchema.safeParse(args)
503 if (!parsedArgs.success) {
504 return writingError(`Invalid args in handleAddBlock: ${formatFirstIssue(parsedArgs.error)}`)
505 }
506 const filePath = parsedArgs.data.path
507 const notebookFilter = parsedArgs.data.notebook
508 const blockSpec = parsedArgs.data.block
509 const position = parsedArgs.data.position
510 const dryRun = parsedArgs.data.dryRun === true
511
512 const file = await loadDeepnoteFile(filePath)
513 if (file.project.notebooks.length === 0) {
514 return {
515 content: [{ type: 'text', text: `No notebooks found for file "${file.project.name}" (${file.project.id})` }],
516 isError: true,
517 }
518 }
519
520 // Find the target notebook
521 let notebook = file.project.notebooks[0]
522 if (notebookFilter) {
523 const found = file.project.notebooks.find(n => n.name === notebookFilter || n.id === notebookFilter)
524 if (!found) {
525 return {
526 content: [{ type: 'text', text: `Notebook not found: ${notebookFilter}` }],
527 isError: true,
528 }
529 }
530 notebook = found
531 }
532
533 // Determine insertion index
534 let insertIndex = notebook.blocks.length
535 if (position?.after) {
536 const afterIdx = findBlockIndex(notebook.blocks, position.after)
537 if (afterIdx < 0) {
538 throw new Error(`Invalid position.after: block not found (${position.after})`)
539 }
540 insertIndex = afterIdx + 1
541 } else if (position?.before) {
542 const beforeIdx = findBlockIndex(notebook.blocks, position.before)
543 if (beforeIdx < 0) {
544 throw new Error(`Invalid position.before: block not found (${position.before})`)
545 }
546 insertIndex = beforeIdx
547 } else if (position?.index !== undefined) {
548 if (!Number.isInteger(position.index) || position.index < 0 || position.index > notebook.blocks.length) {
549 throw new Error(`Invalid position.index: expected integer in range 0..${notebook.blocks.length}`)
550 }
551 insertIndex = position.index
552 }
553
554 // Create the new block
555 const newBlock = createBlock(blockSpec, insertIndex)
556
557 // Insert the block
558 notebook.blocks.splice(insertIndex, 0, newBlock)

Callers 1

handleWritingToolFunction · 0.85

Calls 7

generateSortingKeyFunction · 0.90
writingErrorFunction · 0.85
findBlockIndexFunction · 0.85
saveDeepnoteFileFunction · 0.85
formatFirstIssueFunction · 0.70
createBlockFunction · 0.70
loadDeepnoteFileFunction · 0.50

Tested by

no test coverage detected