-
Notifications
You must be signed in to change notification settings - Fork 79
feat: add search and fetch tool for organizations, projects, and branches #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shridhad
wants to merge
4
commits into
main
Choose a base branch
from
feat/search-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+788
−231
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d76d663
feat: add search tool for organizations, projects, and branches
ab90feb
feat: fetch tool to retrieve details about the org, project, or branch
Shridhad ad74c01
fix: do not fail describe branch if no compute
Shridhad 1ed2e06
feat: generate link to console
Shridhad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Api } from '@neondatabase/api-client'; | ||
import { ToolHandlerExtraParams } from '../types.js'; | ||
import { startSpan } from '@sentry/node'; | ||
import { getDefaultDatabase } from '../utils.js'; | ||
import { getDefaultBranch, getOnlyProject } from './utils.js'; | ||
|
||
export async function handleGetConnectionString( | ||
{ | ||
projectId, | ||
branchId, | ||
computeId, | ||
databaseName, | ||
roleName, | ||
}: { | ||
projectId?: string; | ||
branchId?: string; | ||
computeId?: string; | ||
databaseName?: string; | ||
roleName?: string; | ||
}, | ||
neonClient: Api<unknown>, | ||
extra: ToolHandlerExtraParams, | ||
) { | ||
return await startSpan( | ||
{ | ||
name: 'get_connection_string', | ||
}, | ||
async () => { | ||
// If projectId is not provided, get the first project but only if there is only one project | ||
if (!projectId) { | ||
const project = await getOnlyProject(neonClient, extra); | ||
projectId = project.id; | ||
} | ||
|
||
if (!branchId) { | ||
const defaultBranch = await getDefaultBranch(projectId, neonClient); | ||
branchId = defaultBranch.id; | ||
} | ||
|
||
// If databaseName is not provided, use default `neondb` or first database | ||
let dbObject; | ||
if (!databaseName) { | ||
dbObject = await getDefaultDatabase( | ||
{ | ||
projectId, | ||
branchId, | ||
databaseName, | ||
}, | ||
neonClient, | ||
); | ||
databaseName = dbObject.name; | ||
|
||
if (!roleName) { | ||
roleName = dbObject.owner_name; | ||
} | ||
} else if (!roleName) { | ||
const { data } = await neonClient.getProjectBranchDatabase( | ||
projectId, | ||
branchId, | ||
databaseName, | ||
); | ||
roleName = data.database.owner_name; | ||
} | ||
|
||
// Get connection URI with the provided parameters | ||
const connectionString = await neonClient.getConnectionUri({ | ||
projectId, | ||
role_name: roleName, | ||
database_name: databaseName, | ||
branch_id: branchId, | ||
endpoint_id: computeId, | ||
}); | ||
|
||
return { | ||
uri: connectionString.data.uri, | ||
projectId, | ||
branchId, | ||
databaseName, | ||
roleName, | ||
computeId, | ||
}; | ||
}, | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Api } from '@neondatabase/api-client'; | ||
|
||
async function handleDescribeProject( | ||
projectId: string, | ||
neonClient: Api<unknown>, | ||
) { | ||
const { data: branchesData } = await neonClient.listProjectBranches({ | ||
projectId: projectId, | ||
}); | ||
const { data: projectData } = await neonClient.getProject(projectId); | ||
return { | ||
branches: branchesData.branches, | ||
project: projectData.project, | ||
}; | ||
} | ||
|
||
export { handleDescribeProject }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Api, Branch } from '@neondatabase/api-client'; | ||
import { ToolHandlerExtraParams } from '../types.js'; | ||
import { handleGetConnectionString } from './connection-string.js'; | ||
import { neon } from '@neondatabase/serverless'; | ||
import { DESCRIBE_DATABASE_STATEMENTS } from '../utils.js'; | ||
import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; | ||
import { CONSOLE_URLS, generateConsoleUrl } from './urls.js'; | ||
|
||
const branchInfo = (branch: Branch) => { | ||
return `Branch Details: | ||
Name: ${branch.name} | ||
ID: ${branch.id} | ||
Parent Branch: ${branch.parent_id} | ||
Default: ${branch.default} | ||
Protected: ${branch.protected ? 'Yes' : 'No'} | ||
|
||
${branch.created_by ? `Created By: ${branch.created_by.name}` : ''} | ||
Created: ${new Date(branch.created_at).toLocaleDateString()} | ||
Updated: ${new Date(branch.updated_at).toLocaleDateString()} | ||
|
||
Compute Usage: ${branch.compute_time_seconds} seconds | ||
Written Data: ${branch.written_data_bytes} bytes | ||
Data Transfer: ${branch.data_transfer_bytes} bytes | ||
|
||
Console Link: ${generateConsoleUrl(CONSOLE_URLS.PROJECT_BRANCH, { | ||
projectId: branch.project_id, | ||
branchId: branch.id, | ||
})} | ||
`; | ||
}; | ||
|
||
export async function handleDescribeBranch( | ||
{ | ||
projectId, | ||
databaseName, | ||
branchId, | ||
}: { | ||
projectId: string; | ||
databaseName?: string; | ||
branchId: string; | ||
}, | ||
neonClient: Api<unknown>, | ||
extra: ToolHandlerExtraParams, | ||
): Promise<CallToolResult> { | ||
const { data: branchData } = await neonClient.getProjectBranch( | ||
projectId, | ||
branchId, | ||
); | ||
|
||
const branch = branchData.branch; | ||
|
||
let response: Record<string, any>[][]; | ||
try { | ||
const connectionString = await handleGetConnectionString( | ||
{ | ||
projectId, | ||
branchId: branch.id, | ||
databaseName, | ||
}, | ||
neonClient, | ||
extra, | ||
); | ||
const runQuery = neon(connectionString.uri); | ||
response = await runQuery.transaction( | ||
DESCRIBE_DATABASE_STATEMENTS.map((sql) => runQuery.query(sql)), | ||
); | ||
|
||
return { | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: branchInfo(branch), | ||
metadata: branch, | ||
}, | ||
{ | ||
type: 'text', | ||
text: ['Database Structure:', JSON.stringify(response, null, 2)].join( | ||
'\n', | ||
), | ||
databasetree: response, | ||
}, | ||
], | ||
}; | ||
} catch { | ||
// Ignore database connection errors | ||
} | ||
|
||
return { | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: branchInfo(branch), | ||
}, | ||
], | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that this tool can only be used with the
search
tool combined?