Skip to content

Commit c406f5a

Browse files
committed
feat: Add MCP Demo page and integrate into MainLayout
- Added a new page for Model Context Protocol (MCP) demo with tools and prompts testing functionality. - Updated MainLayout to include a link to the new MCP Demo page. - Implemented CORS configuration in Program.cs for enhanced security and cross-origin requests. - Updated appsettings.json to include CORS settings and API keys for Azure services. - Created a comprehensive manual testing guide for the NLWebNet API with sample requests. - Added sample requests for testing the NLWebNet API endpoints. - Enhanced the TODO documentation to reflect the completion of phases and project status. - Implemented unit tests for Minimal API endpoints to ensure proper registration and parameter binding. - Fixed minor exception handling in AskEndpoints for better logging.
1 parent f9839eb commit c406f5a

File tree

10 files changed

+1133
-39
lines changed

10 files changed

+1133
-39
lines changed

demo/Components/Layout/MainLayout.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
@inherits LayoutComponentBase
22

33
<div class="page">
4-
<main>
5-
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
4+
<main> <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
65
<div class="container">
76
<a class="navbar-brand" href="/">NLWebNet Demo</a>
87
<div class="navbar-nav">
@@ -15,6 +14,9 @@
1514
<a class="nav-link" href="/api-test">
1615
<i class="fas fa-flask me-1"></i>API Test
1716
</a>
17+
<a class="nav-link" href="/mcp-demo">
18+
<i class="fas fa-tools me-1"></i>MCP Demo
19+
</a>
1820
<a class="nav-link" href="/openapi/v1.json" target="_blank">
1921
<i class="fas fa-file-code me-1"></i>OpenAPI Spec
2022
</a>

demo/Components/Pages/McpDemo.razor

Lines changed: 486 additions & 0 deletions
Large diffs are not rendered by default.

demo/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88
builder.Services.AddRazorComponents()
99
.AddInteractiveServerComponents();
1010

11+
// Add CORS configuration
12+
builder.Services.AddCors(options =>
13+
{
14+
options.AddDefaultPolicy(corsBuilder =>
15+
{
16+
var corsSettings = builder.Configuration.GetSection("CORS");
17+
var allowedOrigins = corsSettings.GetSection("AllowedOrigins").Get<string[]>() ?? new[] { "*" };
18+
var allowedMethods = corsSettings.GetSection("AllowedMethods").Get<string[]>() ?? new[] { "GET", "POST", "OPTIONS" };
19+
var allowedHeaders = corsSettings.GetSection("AllowedHeaders").Get<string[]>() ?? new[] { "Content-Type", "Authorization" };
20+
var allowCredentials = corsSettings.GetValue<bool>("AllowCredentials");
21+
22+
corsBuilder.WithOrigins(allowedOrigins)
23+
.WithMethods(allowedMethods)
24+
.WithHeaders(allowedHeaders);
25+
26+
if (allowCredentials)
27+
corsBuilder.AllowCredentials();
28+
});
29+
});
30+
1131
// Add NLWebNet services
1232
builder.Services.AddNLWebNet(options =>
1333
{
@@ -35,6 +55,9 @@
3555

3656
app.UseHttpsRedirection();
3757

58+
// Enable CORS
59+
app.UseCors();
60+
3861
// Add NLWebNet middleware
3962
app.UseNLWebNet();
4063

demo/appsettings.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,36 @@
88
"AzureOpenAI": {
99
"Endpoint": "",
1010
"DeploymentName": "gpt-4",
11-
"ApiVersion": "2024-02-01"
11+
"ApiVersion": "2024-02-01",
12+
"ApiKey": ""
13+
},
14+
"OpenAI": {
15+
"ApiKey": "",
16+
"Model": "gpt-4",
17+
"BaseUrl": "https://api.openai.com/v1"
1218
},
1319
"AzureSearch": {
1420
"ServiceName": "",
15-
"IndexName": "nlweb-index"
21+
"IndexName": "nlweb-index",
22+
"ApiKey": ""
23+
},
24+
"CORS": {
25+
"AllowedOrigins": [
26+
"http://localhost:3000",
27+
"http://localhost:5173",
28+
"https://localhost:7073"
29+
],
30+
"AllowedMethods": [
31+
"GET",
32+
"POST",
33+
"OPTIONS"
34+
],
35+
"AllowedHeaders": [
36+
"Content-Type",
37+
"Authorization",
38+
"Accept"
39+
],
40+
"AllowCredentials": true
1641
},
1742
"Logging": {
1843
"LogLevel": {

doc/manual-testing-guide.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# Manual Testing Guide for NLWebNet API
2+
3+
This guide provides sample requests for manual testing of the NLWebNet API endpoints.
4+
5+
## Prerequisites
6+
7+
1. Start the demo application:
8+
9+
```bash
10+
cd demo
11+
dotnet run
12+
```
13+
14+
The application will be available at `http://localhost:5037`
15+
16+
## Testing the /ask Endpoint
17+
18+
### 1. Basic GET Request - List Mode
19+
20+
```bash
21+
curl "http://localhost:5037/ask?query=artificial%20intelligence&mode=List"
22+
```
23+
24+
### 2. POST Request with JSON Body - Summarize Mode
25+
26+
```bash
27+
curl -X POST "http://localhost:5037/ask" \
28+
-H "Content-Type: application/json" \
29+
-d '{
30+
"query": "machine learning algorithms",
31+
"mode": "Summarize",
32+
"site": "example",
33+
"streaming": false
34+
}'
35+
```
36+
37+
### 3. Generate Mode with Previous Context
38+
39+
```bash
40+
curl -X POST "http://localhost:5037/ask" \
41+
-H "Content-Type: application/json" \
42+
-d '{
43+
"query": "What are the latest developments?",
44+
"mode": "Generate",
45+
"prev": ["artificial intelligence", "machine learning"],
46+
"decontextualized_query": "What are the latest developments in artificial intelligence and machine learning?",
47+
"streaming": false
48+
}'
49+
```
50+
51+
### 4. Streaming Request
52+
53+
```bash
54+
curl "http://localhost:5037/ask?query=deep%20learning&streaming=true"
55+
```
56+
57+
## Testing the /mcp Endpoint
58+
59+
### 1. Basic MCP Request
60+
61+
```bash
62+
curl -X POST "http://localhost:5037/mcp" \
63+
-H "Content-Type: application/json" \
64+
-d '{
65+
"query": "neural networks",
66+
"mode": "List",
67+
"streaming": false
68+
}'
69+
```
70+
71+
### 2. MCP with Site Filter
72+
73+
```bash
74+
curl -X POST "http://localhost:5037/mcp" \
75+
-H "Content-Type: application/json" \
76+
-d '{
77+
"query": "computer vision",
78+
"mode": "Summarize",
79+
"site": "research",
80+
"streaming": false
81+
}'
82+
```
83+
84+
## Testing MCP Protocol Endpoints
85+
86+
### 1. List Available Tools
87+
88+
```bash
89+
curl "http://localhost:5037/mcp/list_tools"
90+
```
91+
92+
### 2. List Available Prompts
93+
94+
```bash
95+
curl "http://localhost:5037/mcp/list_prompts"
96+
```
97+
98+
### 3. Call nlweb_search Tool
99+
100+
```bash
101+
curl -X POST "http://localhost:5037/mcp/call_tool" \
102+
-H "Content-Type: application/json" \
103+
-d '{
104+
"name": "nlweb_search",
105+
"arguments": {
106+
"query": "natural language processing",
107+
"mode": "list",
108+
"site": "papers"
109+
}
110+
}'
111+
```
112+
113+
### 4. Call nlweb_query_history Tool
114+
115+
```bash
116+
curl -X POST "http://localhost:5037/mcp/call_tool" \
117+
-H "Content-Type: application/json" \
118+
-d '{
119+
"name": "nlweb_query_history",
120+
"arguments": {
121+
"query": "What are transformers?",
122+
"previous_queries": ["machine learning", "neural networks", "deep learning"]
123+
}
124+
}'
125+
```
126+
127+
### 5. Get Search Prompt
128+
129+
```bash
130+
curl -X POST "http://localhost:5037/mcp/get_prompt" \
131+
-H "Content-Type: application/json" \
132+
-d '{
133+
"name": "nlweb_search_prompt",
134+
"arguments": {
135+
"query": "quantum computing",
136+
"context": "research papers"
137+
}
138+
}'
139+
```
140+
141+
### 6. Get Summarize Prompt
142+
143+
```bash
144+
curl -X POST "http://localhost:5037/mcp/get_prompt" \
145+
-H "Content-Type: application/json" \
146+
-d '{
147+
"name": "nlweb_summarize_prompt",
148+
"arguments": {
149+
"results": "[{\"title\": \"Quantum Computing Basics\", \"content\": \"...\"}]",
150+
"query": "quantum computing fundamentals"
151+
}
152+
}'
153+
```
154+
155+
## Testing OpenAPI Documentation
156+
157+
### 1. Get OpenAPI Schema
158+
159+
```bash
160+
curl "http://localhost:5037/openapi/v1.json"
161+
```
162+
163+
## Error Testing
164+
165+
### 1. Missing Query Parameter
166+
167+
```bash
168+
curl "http://localhost:5037/ask"
169+
# Expected: 400 Bad Request
170+
```
171+
172+
### 2. Invalid Mode
173+
174+
```bash
175+
curl "http://localhost:5037/ask?query=test&mode=InvalidMode"
176+
# Expected: 400 Bad Request (depending on validation)
177+
```
178+
179+
### 3. Unknown MCP Tool
180+
181+
```bash
182+
curl -X POST "http://localhost:5037/mcp/call_tool" \
183+
-H "Content-Type: application/json" \
184+
-d '{
185+
"name": "unknown_tool",
186+
"arguments": {}
187+
}'
188+
# Expected: Tool not found error
189+
```
190+
191+
## CORS Testing
192+
193+
### 1. Preflight Request
194+
195+
```bash
196+
curl -X OPTIONS "http://localhost:5037/ask" \
197+
-H "Origin: http://localhost:3000" \
198+
-H "Access-Control-Request-Method: POST" \
199+
-H "Access-Control-Request-Headers: Content-Type"
200+
```
201+
202+
### 2. Cross-Origin Request
203+
204+
```bash
205+
curl -X POST "http://localhost:5037/ask" \
206+
-H "Origin: http://localhost:3000" \
207+
-H "Content-Type: application/json" \
208+
-d '{"query": "test", "streaming": false}'
209+
```
210+
211+
## Expected Response Formats
212+
213+
### NLWeb Response Structure
214+
215+
```json
216+
{
217+
"queryId": "string",
218+
"query": "string",
219+
"mode": "List|Summarize|Generate",
220+
"results": [
221+
{
222+
"url": "string",
223+
"name": "string",
224+
"site": "string",
225+
"score": 0.95,
226+
"description": "string",
227+
"schemaObject": {}
228+
}
229+
],
230+
"summary": "string (for Summarize mode)",
231+
"answer": "string (for Generate mode)"
232+
}
233+
```
234+
235+
### MCP Tool Response Structure
236+
237+
```json
238+
{
239+
"content": [
240+
{
241+
"type": "text",
242+
"text": "Response content"
243+
}
244+
]
245+
}
246+
```
247+
248+
### MCP Error Response Structure
249+
250+
```json
251+
{
252+
"error": {
253+
"code": "TOOL_NOT_FOUND",
254+
"message": "Tool 'unknown_tool' not found"
255+
}
256+
}
257+
```
258+
259+
## Performance Testing
260+
261+
### 1. Load Testing with Multiple Concurrent Requests
262+
263+
```bash
264+
# Using Apache Bench (if available)
265+
ab -n 100 -c 10 "http://localhost:5037/ask?query=test&streaming=false"
266+
```
267+
268+
### 2. Streaming Performance
269+
270+
```bash
271+
# Test streaming response time
272+
time curl "http://localhost:5037/ask?query=long%20query%20for%20testing&streaming=true"
273+
```
274+
275+
## Notes
276+
277+
- All endpoints support both GET and POST methods where applicable
278+
- Streaming responses use Server-Sent Events format
279+
- JSON responses are properly formatted with appropriate content types
280+
- Error responses include proper HTTP status codes
281+
- OpenAPI documentation is available for API exploration

0 commit comments

Comments
 (0)