-
Notifications
You must be signed in to change notification settings - Fork 679
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
added aws kb server #180
Merged
jspahrsummers
merged 5 commits into
modelcontextprotocol:main
from
Skirano:aws-kb-retrieval
Dec 5, 2024
Merged
added aws kb server #180
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b349488
added aws kb server
Skirano 07b47bc
fixed version and updated the server list in the main Readme
Skirano 2d002fa
Merge branch 'main' into aws-kb-retrieval
jspahrsummers 5020b4b
Update package.json
jspahrsummers bd4a101
`npm install`
jspahrsummers 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 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,53 @@ | ||
# AWS Knowledge Base Retrieval MCP Server | ||
|
||
An MCP server implementation for retrieving information from the AWS Knowledge Base using the Bedrock Agent Runtime. | ||
|
||
## Features | ||
|
||
- **RAG (Retrieval-Augmented Generation)**: Retrieve context from the AWS Knowledge Base based on a query and a Knowledge Base ID. | ||
- **Supports multiple results retrieval**: Option to retrieve a customizable number of results. | ||
|
||
## Tools | ||
|
||
- **retrieve_from_aws_kb** | ||
- Perform retrieval operations using the AWS Knowledge Base. | ||
- Inputs: | ||
- `query` (string): The search query for retrieval. | ||
- `knowledgeBaseId` (string): The ID of the AWS Knowledge Base. | ||
- `n` (number, optional): Number of results to retrieve (default: 3). | ||
|
||
## Configuration | ||
|
||
### Setting up AWS Credentials | ||
|
||
1. Obtain AWS access key ID, secret access key, and region from the AWS Management Console. | ||
2. Ensure these credentials have appropriate permissions for Bedrock Agent Runtime operations. | ||
|
||
### Usage with Claude Desktop | ||
|
||
Add this to your `claude_desktop_config.json`: | ||
|
||
```json | ||
{ | ||
"mcpServers": { | ||
"aws-kb-retrieval": { | ||
"command": "npx", | ||
"args": [ | ||
"-y", | ||
"@modelcontextprotocol/server-aws-kb-retrieval" | ||
], | ||
"env": { | ||
"AWS_ACCESS_KEY_ID": "YOUR_ACCESS_KEY_HERE", | ||
"AWS_SECRET_ACCESS_KEY": "YOUR_SECRET_ACCESS_KEY_HERE", | ||
"AWS_REGION": "YOUR_AWS_REGION_HERE" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## License | ||
|
||
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. | ||
|
||
This README assumes that your server package is named `@modelcontextprotocol/server-aws-kb-retrieval`. Adjust the package name and installation details if they differ in your setup. Also, ensure that your server script is correctly built and that all dependencies are properly managed in your `package.json`. |
This file contains 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,166 @@ | ||
#!/usr/bin/env node | ||
import { Server } from "@modelcontextprotocol/sdk/server/index.js"; | ||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | ||
import { | ||
CallToolRequestSchema, | ||
ListToolsRequestSchema, | ||
Tool, | ||
} from "@modelcontextprotocol/sdk/types.js"; | ||
import { | ||
BedrockAgentRuntimeClient, | ||
RetrieveCommand, | ||
RetrieveCommandInput, | ||
} from "@aws-sdk/client-bedrock-agent-runtime"; | ||
|
||
// AWS client initialization | ||
const bedrockClient = new BedrockAgentRuntimeClient({ | ||
region: process.env.AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID!, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!, | ||
}, | ||
}); | ||
|
||
interface RAGSource { | ||
id: string; | ||
fileName: string; | ||
snippet: string; | ||
score: number; | ||
} | ||
|
||
async function retrieveContext( | ||
query: string, | ||
knowledgeBaseId: string, | ||
n: number = 3 | ||
): Promise<{ | ||
context: string; | ||
isRagWorking: boolean; | ||
ragSources: RAGSource[]; | ||
}> { | ||
try { | ||
if (!knowledgeBaseId) { | ||
console.error("knowledgeBaseId is not provided"); | ||
return { | ||
context: "", | ||
isRagWorking: false, | ||
ragSources: [], | ||
}; | ||
} | ||
|
||
const input: RetrieveCommandInput = { | ||
knowledgeBaseId: knowledgeBaseId, | ||
retrievalQuery: { text: query }, | ||
retrievalConfiguration: { | ||
vectorSearchConfiguration: { numberOfResults: n }, | ||
}, | ||
}; | ||
|
||
const command = new RetrieveCommand(input); | ||
const response = await bedrockClient.send(command); | ||
const rawResults = response?.retrievalResults || []; | ||
const ragSources: RAGSource[] = rawResults | ||
.filter((res) => res?.content?.text) | ||
.map((result, index) => { | ||
const uri = result?.location?.s3Location?.uri || ""; | ||
const fileName = uri.split("/").pop() || `Source-${index}.txt`; | ||
return { | ||
id: (result.metadata?.["x-amz-bedrock-kb-chunk-id"] as string) || `chunk-${index}`, | ||
fileName: fileName.replace(/_/g, " ").replace(".txt", ""), | ||
snippet: result.content?.text || "", | ||
score: (result.score as number) || 0, | ||
}; | ||
}) | ||
.slice(0, 3); | ||
|
||
const context = rawResults | ||
.filter((res): res is { content: { text: string } } => res?.content?.text !== undefined) | ||
.map(res => res.content.text) | ||
.join("\n\n"); | ||
|
||
return { | ||
context, | ||
isRagWorking: true, | ||
ragSources, | ||
}; | ||
} catch (error) { | ||
console.error("RAG Error:", error); | ||
return { context: "", isRagWorking: false, ragSources: [] }; | ||
} | ||
} | ||
|
||
// Define the retrieval tool | ||
const RETRIEVAL_TOOL: Tool = { | ||
name: "retrieve_from_aws_kb", | ||
description: "Performs retrieval from the AWS Knowledge Base using the provided query and Knowledge Base ID.", | ||
inputSchema: { | ||
type: "object", | ||
properties: { | ||
query: { type: "string", description: "The query to perform retrieval on" }, | ||
knowledgeBaseId: { type: "string", description: "The ID of the AWS Knowledge Base" }, | ||
n: { type: "number", default: 3, description: "Number of results to retrieve" }, | ||
}, | ||
required: ["query", "knowledgeBaseId"], | ||
}, | ||
}; | ||
|
||
// Server setup | ||
const server = new Server( | ||
{ | ||
name: "aws-kb-retrieval-server", | ||
version: "0.2.0", | ||
}, | ||
{ | ||
capabilities: { | ||
tools: {}, | ||
}, | ||
}, | ||
); | ||
|
||
// Request handlers | ||
server.setRequestHandler(ListToolsRequestSchema, async () => ({ | ||
tools: [RETRIEVAL_TOOL], | ||
})); | ||
|
||
server.setRequestHandler(CallToolRequestSchema, async (request) => { | ||
const { name, arguments: args } = request.params; | ||
|
||
if (name === "retrieve_from_aws_kb") { | ||
const { query, knowledgeBaseId, n = 3 } = args as Record<string, any>; | ||
try { | ||
const result = await retrieveContext(query, knowledgeBaseId, n); | ||
if (result.isRagWorking) { | ||
return { | ||
content: [ | ||
{ type: "text", text: `Context: ${result.context}` }, | ||
{ type: "text", text: `RAG Sources: ${JSON.stringify(result.ragSources)}` }, | ||
], | ||
}; | ||
} else { | ||
return { | ||
content: [{ type: "text", text: "Retrieval failed or returned no results." }], | ||
}; | ||
} | ||
} catch (error) { | ||
return { | ||
content: [{ type: "text", text: `Error occurred: ${error}` }], | ||
}; | ||
} | ||
} else { | ||
return { | ||
content: [{ type: "text", text: `Unknown tool: ${name}` }], | ||
isError: true, | ||
}; | ||
} | ||
}); | ||
|
||
// Server startup | ||
async function runServer() { | ||
const transport = new StdioServerTransport(); | ||
await server.connect(transport); | ||
console.error("AWS KB Retrieval Server running on stdio"); | ||
} | ||
|
||
runServer().catch((error) => { | ||
console.error("Fatal error running server:", error); | ||
process.exit(1); | ||
}); |
This file contains 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,30 @@ | ||
{ | ||
"name": "@modelcontextprotocol/server-aws-kb-retrieval", | ||
"version": "0.1.0", | ||
"description": "MCP server for AWS Knowledge Base retrieval using Bedrock Agent Runtime", | ||
"license": "MIT", | ||
"author": "Anthropic, PBC (https://anthropic.com)", | ||
"homepage": "https://modelcontextprotocol.io", | ||
"bugs": "https://github.com/modelcontextprotocol/servers/issues", | ||
"type": "module", | ||
"bin": { | ||
"mcp-server-aws-kb-retrieval": "dist/index.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsc && shx chmod +x dist/*.js", | ||
"prepare": "npm run build", | ||
"watch": "tsc --watch" | ||
}, | ||
"dependencies": { | ||
"@modelcontextprotocol/sdk": "0.5.0", | ||
"@aws-sdk/client-bedrock-agent-runtime": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.10.0", | ||
"shx": "^0.3.4", | ||
"typescript": "^5.6.2" | ||
} | ||
} |
This file contains 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 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": ".", | ||
"composite": true, | ||
"incremental": true, | ||
"tsBuildInfoFile": "./dist/.tsbuildinfo" | ||
}, | ||
"include": [ | ||
"./**/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
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.
Can you please match the version to the rest of the servers? They're released in batch right now (although we want to change this).
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.
done!