Repository navigation
feat(search-mcp): look up people in the organization directory #10435
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
Merged
+34,387
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad32c7b
feat(search-mcp): look up people in the organization directory
waleedlatif1 93fb876
fix(search-mcp): validate people lookup arguments and commit before v…
waleedlatif1 e60dc19
fix(search-mcp): state the people search word cap and always send rep…
waleedlatif1 c68f328
chore(search-mcp): drop a benchmark-only preload committed by mistake
waleedlatif1 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,165 @@ | ||
| import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' | ||
| import type { Principal } from '@sim/auth/principal' | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import type { NextRequest } from 'next/server' | ||
| import { | ||
| organizationKnowledgeMcpContract, | ||
| searchPeopleMcpSchema, | ||
| } from '@/lib/api/contracts/knowledge/mcp' | ||
| import type { PersonSummary } from '@/lib/api/contracts/people' | ||
| import { cursorRoute, cursorScopeKey } from '@/lib/api/cursor-binding' | ||
| import { OrchestrationError } from '@/lib/core/orchestration/types' | ||
| import { | ||
| KNOWLEDGE_MCP_READ_ONLY, | ||
| type KnowledgeMcpToolRunner, | ||
| projectResult, | ||
| } from '@/lib/knowledge/mcp/tool-runner' | ||
| import { getPerson, listPeople } from '@/lib/people/application/use-cases' | ||
| import { isPeopleEnabled } from '@/lib/people/feature-flag' | ||
| import type { PersonRecord } from '@/lib/people/repository' | ||
| import { readSortedCursor, writeSortedCursor } from '@/app/api/v2/lib/response' | ||
|
|
||
| const logger = createLogger('KnowledgeMcpPeople') | ||
|
|
||
| /** Said when a query matches no one, so a client looks for people outside the organization in search. */ | ||
| export const NO_PEOPLE_MATCH_NOTE = | ||
| 'No one in this organization’s directory matches. The directory lists only people in this organization; use search for anyone else, such as customer or vendor contacts.' | ||
|
|
||
| /** | ||
| * Whether `principal` may look people up in `organizationId`: People is on there and the | ||
| * directory is open to them. Asked when listing tools, so a client whose organization has no | ||
| * People directory, or who may not browse it, is never offered the tool. A lookup that fails hides | ||
| * the tool rather than failing the listing around it. | ||
| */ | ||
| export async function canSearchPeople( | ||
| principal: Principal, | ||
| organizationId: string | ||
| ): Promise<boolean> { | ||
| try { | ||
| if (!(await isPeopleEnabled(organizationId))) return false | ||
| await listPeople.authorize({ principal, input: { organizationId } }) | ||
| return true | ||
| } catch (error) { | ||
| if (!(error instanceof OrchestrationError)) | ||
| logger.warn('Could not resolve Search MCP people access', { | ||
| organizationId, | ||
| error: getErrorMessage(error), | ||
| }) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function summaryFacts(person: PersonSummary) { | ||
| return { | ||
| personId: person.memberId, | ||
| name: person.name, | ||
| ...(person.title ? { title: person.title } : {}), | ||
| } | ||
| } | ||
|
|
||
| function reportFacts(report: PersonSummary & { department: string | null; reportCount: number }) { | ||
| return { | ||
| ...summaryFacts(report), | ||
| ...(report.department ? { department: report.department } : {}), | ||
| directReports: report.reportCount, | ||
| } | ||
| } | ||
|
|
||
| function personFacts(person: PersonRecord) { | ||
| const { department, location, startDate } = person | ||
| return { | ||
| ...summaryFacts(person), | ||
| email: person.email, | ||
| ...(department ? { department } : {}), | ||
| ...(location ? { location } : {}), | ||
| ...(startDate ? { startDate } : {}), | ||
| } | ||
| } | ||
|
|
||
| interface SearchPeopleToolContext { | ||
| execute: KnowledgeMcpToolRunner | ||
| principal: Principal | ||
| organizationId: string | ||
| request: NextRequest | ||
| } | ||
|
|
||
| /** | ||
| * Registers `search_people`: the organization's People directory through the same use cases the | ||
| * People page reads. A query that finds one person, or a personId, returns that person with their | ||
| * reporting line, so "who is X's manager" or "who reports to X" takes one call. | ||
| */ | ||
| export function registerSearchPeopleTool( | ||
| server: McpServer, | ||
| { execute, principal, organizationId, request }: SearchPeopleToolContext | ||
| ) { | ||
| const detail = async (memberId: string) => { | ||
| const { person, chain, reports, totalReports } = await getPerson.execute({ | ||
| principal, | ||
| input: { organizationId, memberId }, | ||
| request, | ||
| }) | ||
| return { | ||
| person: { | ||
| ...personFacts(person), | ||
| ...(chain.length ? { managerChain: chain.map(summaryFacts) } : {}), | ||
| ...(reports.length ? { directReports: reports.map(reportFacts) } : {}), | ||
| totalDirectReports: totalReports, | ||
|
waleedlatif1 marked this conversation as resolved.
Resolved
|
||
| ...(totalReports > reports.length | ||
| ? { | ||
| note: `directReports lists the first ${reports.length} of ${totalReports}; search by department or title to find the others.`, | ||
| } | ||
| : {}), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| server.registerTool( | ||
| 'search_people', | ||
| { | ||
| title: 'Search people', | ||
| description: | ||
| 'Look up people in this organization’s directory by name, email, title, department, or location. Returns each person’s title, department, location, email, and manager. When exactly one person matches, or with a personId, returns that person with managerChain (direct manager first) and directReports. Covers only this organization’s people; use search for customers, vendors, and other outside contacts.', | ||
| inputSchema: searchPeopleMcpSchema, | ||
| annotations: KNOWLEDGE_MCP_READ_ONLY, | ||
| }, | ||
| async (input: unknown, extra: { signal: AbortSignal }) => { | ||
| const personId = searchPeopleMcpSchema.safeParse(input).data?.personId | ||
| if (personId) | ||
| return execute('search_people', getPerson.operation, extra.signal, async (registry) => | ||
| projectResult(await detail(personId), registry) | ||
| ) | ||
| return execute('search_people', listPeople.operation, extra.signal, async (registry) => { | ||
| const { query, cursor } = searchPeopleMcpSchema.parse(input) | ||
| const scope = cursorScopeKey( | ||
| cursorRoute(organizationKnowledgeMcpContract, { organizationId }), | ||
| { tool: 'search_people', query } | ||
| ) | ||
| const page = await listPeople.execute({ | ||
| principal, | ||
| input: { | ||
| organizationId, | ||
| search: query, | ||
| cursorKeys: readSortedCursor(cursor, 'name', 'asc', scope), | ||
| }, | ||
| request, | ||
| }) | ||
| const [only] = page.data | ||
| if (only && page.data.length === 1 && !cursor) | ||
| return projectResult(await detail(only.memberId), registry) | ||
| const nextCursor = writeSortedCursor(page.nextCursorKeys, 'name', 'asc', scope) | ||
| return projectResult( | ||
| { | ||
| people: page.data.map((person) => ({ | ||
| ...personFacts(person), | ||
| ...(person.manager ? { manager: person.manager.name } : {}), | ||
| })), | ||
| ...(nextCursor ? { nextCursor } : {}), | ||
| ...(page.data.length ? {} : { note: NO_PEOPLE_MATCH_NOTE }), | ||
| }, | ||
| registry | ||
| ) | ||
| }) | ||
| } | ||
| ) | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.