Skip to content

Commit b349488

Browse files
committed
added aws kb server
1 parent dac09b2 commit b349488

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

src/aws-kb-retrieval-server/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# AWS Knowledge Base Retrieval MCP Server
2+
3+
An MCP server implementation for retrieving information from the AWS Knowledge Base using the Bedrock Agent Runtime.
4+
5+
## Features
6+
7+
- **RAG (Retrieval-Augmented Generation)**: Retrieve context from the AWS Knowledge Base based on a query and a Knowledge Base ID.
8+
- **Supports multiple results retrieval**: Option to retrieve a customizable number of results.
9+
10+
## Tools
11+
12+
- **retrieve_from_aws_kb**
13+
- Perform retrieval operations using the AWS Knowledge Base.
14+
- Inputs:
15+
- `query` (string): The search query for retrieval.
16+
- `knowledgeBaseId` (string): The ID of the AWS Knowledge Base.
17+
- `n` (number, optional): Number of results to retrieve (default: 3).
18+
19+
## Configuration
20+
21+
### Setting up AWS Credentials
22+
23+
1. Obtain AWS access key ID, secret access key, and region from the AWS Management Console.
24+
2. Ensure these credentials have appropriate permissions for Bedrock Agent Runtime operations.
25+
26+
### Usage with Claude Desktop
27+
28+
Add this to your `claude_desktop_config.json`:
29+
30+
```json
31+
{
32+
"mcpServers": {
33+
"aws-kb-retrieval": {
34+
"command": "npx",
35+
"args": [
36+
"-y",
37+
"@modelcontextprotocol/server-aws-kb-retrieval"
38+
],
39+
"env": {
40+
"AWS_ACCESS_KEY_ID": "YOUR_ACCESS_KEY_HERE",
41+
"AWS_SECRET_ACCESS_KEY": "YOUR_SECRET_ACCESS_KEY_HERE",
42+
"AWS_REGION": "YOUR_AWS_REGION_HERE"
43+
}
44+
}
45+
}
46+
}
47+
```
48+
49+
## License
50+
51+
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.
52+
53+
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`.

src/aws-kb-retrieval-server/index.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env node
2+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4+
import {
5+
CallToolRequestSchema,
6+
ListToolsRequestSchema,
7+
Tool,
8+
} from "@modelcontextprotocol/sdk/types.js";
9+
import {
10+
BedrockAgentRuntimeClient,
11+
RetrieveCommand,
12+
RetrieveCommandInput,
13+
} from "@aws-sdk/client-bedrock-agent-runtime";
14+
15+
// AWS client initialization
16+
const bedrockClient = new BedrockAgentRuntimeClient({
17+
region: process.env.AWS_REGION,
18+
credentials: {
19+
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
20+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
21+
},
22+
});
23+
24+
interface RAGSource {
25+
id: string;
26+
fileName: string;
27+
snippet: string;
28+
score: number;
29+
}
30+
31+
async function retrieveContext(
32+
query: string,
33+
knowledgeBaseId: string,
34+
n: number = 3
35+
): Promise<{
36+
context: string;
37+
isRagWorking: boolean;
38+
ragSources: RAGSource[];
39+
}> {
40+
try {
41+
if (!knowledgeBaseId) {
42+
console.error("knowledgeBaseId is not provided");
43+
return {
44+
context: "",
45+
isRagWorking: false,
46+
ragSources: [],
47+
};
48+
}
49+
50+
const input: RetrieveCommandInput = {
51+
knowledgeBaseId: knowledgeBaseId,
52+
retrievalQuery: { text: query },
53+
retrievalConfiguration: {
54+
vectorSearchConfiguration: { numberOfResults: n },
55+
},
56+
};
57+
58+
const command = new RetrieveCommand(input);
59+
const response = await bedrockClient.send(command);
60+
const rawResults = response?.retrievalResults || [];
61+
const ragSources: RAGSource[] = rawResults
62+
.filter((res) => res?.content?.text)
63+
.map((result, index) => {
64+
const uri = result?.location?.s3Location?.uri || "";
65+
const fileName = uri.split("/").pop() || `Source-${index}.txt`;
66+
return {
67+
id: (result.metadata?.["x-amz-bedrock-kb-chunk-id"] as string) || `chunk-${index}`,
68+
fileName: fileName.replace(/_/g, " ").replace(".txt", ""),
69+
snippet: result.content?.text || "",
70+
score: (result.score as number) || 0,
71+
};
72+
})
73+
.slice(0, 3);
74+
75+
const context = rawResults
76+
.filter((res): res is { content: { text: string } } => res?.content?.text !== undefined)
77+
.map(res => res.content.text)
78+
.join("\n\n");
79+
80+
return {
81+
context,
82+
isRagWorking: true,
83+
ragSources,
84+
};
85+
} catch (error) {
86+
console.error("RAG Error:", error);
87+
return { context: "", isRagWorking: false, ragSources: [] };
88+
}
89+
}
90+
91+
// Define the retrieval tool
92+
const RETRIEVAL_TOOL: Tool = {
93+
name: "retrieve_from_aws_kb",
94+
description: "Performs retrieval from the AWS Knowledge Base using the provided query and Knowledge Base ID.",
95+
inputSchema: {
96+
type: "object",
97+
properties: {
98+
query: { type: "string", description: "The query to perform retrieval on" },
99+
knowledgeBaseId: { type: "string", description: "The ID of the AWS Knowledge Base" },
100+
n: { type: "number", default: 3, description: "Number of results to retrieve" },
101+
},
102+
required: ["query", "knowledgeBaseId"],
103+
},
104+
};
105+
106+
// Server setup
107+
const server = new Server(
108+
{
109+
name: "aws-kb-retrieval-server",
110+
version: "0.2.0",
111+
},
112+
{
113+
capabilities: {
114+
tools: {},
115+
},
116+
},
117+
);
118+
119+
// Request handlers
120+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
121+
tools: [RETRIEVAL_TOOL],
122+
}));
123+
124+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
125+
const { name, arguments: args } = request.params;
126+
127+
if (name === "retrieve_from_aws_kb") {
128+
const { query, knowledgeBaseId, n = 3 } = args as Record<string, any>;
129+
try {
130+
const result = await retrieveContext(query, knowledgeBaseId, n);
131+
if (result.isRagWorking) {
132+
return {
133+
content: [
134+
{ type: "text", text: `Context: ${result.context}` },
135+
{ type: "text", text: `RAG Sources: ${JSON.stringify(result.ragSources)}` },
136+
],
137+
};
138+
} else {
139+
return {
140+
content: [{ type: "text", text: "Retrieval failed or returned no results." }],
141+
};
142+
}
143+
} catch (error) {
144+
return {
145+
content: [{ type: "text", text: `Error occurred: ${error}` }],
146+
};
147+
}
148+
} else {
149+
return {
150+
content: [{ type: "text", text: `Unknown tool: ${name}` }],
151+
isError: true,
152+
};
153+
}
154+
});
155+
156+
// Server startup
157+
async function runServer() {
158+
const transport = new StdioServerTransport();
159+
await server.connect(transport);
160+
console.error("AWS KB Retrieval Server running on stdio");
161+
}
162+
163+
runServer().catch((error) => {
164+
console.error("Fatal error running server:", error);
165+
process.exit(1);
166+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@modelcontextprotocol/server-aws-kb-retrieval",
3+
"version": "0.1.0",
4+
"description": "MCP server for AWS Knowledge Base retrieval using Bedrock Agent Runtime",
5+
"license": "MIT",
6+
"author": "Anthropic, PBC (https://anthropic.com)",
7+
"homepage": "https://modelcontextprotocol.io",
8+
"bugs": "https://github.com/modelcontextprotocol/servers/issues",
9+
"type": "module",
10+
"bin": {
11+
"mcp-server-aws-kb-retrieval": "dist/index.js"
12+
},
13+
"files": [
14+
"dist"
15+
],
16+
"scripts": {
17+
"build": "tsc && shx chmod +x dist/*.js",
18+
"prepare": "npm run build",
19+
"watch": "tsc --watch"
20+
},
21+
"dependencies": {
22+
"@modelcontextprotocol/sdk": "0.5.0",
23+
"@aws-sdk/client-bedrock-agent-runtime": "^3.0.0"
24+
},
25+
"devDependencies": {
26+
"@types/node": "^20.10.0",
27+
"shx": "^0.3.4",
28+
"typescript": "^5.6.2"
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"rootDir": ".",
6+
"composite": true,
7+
"incremental": true,
8+
"tsBuildInfoFile": "./dist/.tsbuildinfo"
9+
},
10+
"include": [
11+
"./**/*.ts"
12+
],
13+
"exclude": [
14+
"node_modules",
15+
"dist"
16+
]
17+
}

0 commit comments

Comments
 (0)