Skip to content

Commit 48fb199

Browse files
committed
Bonus lab 24: MCP
1 parent 124aa66 commit 48fb199

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed

bruno/mcp/1_initialize.bru

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
meta {
2+
name: 1. Initialize Session
3+
type: http
4+
seq: 1
5+
}
6+
7+
post {
8+
url: http://localhost:9000/mcp
9+
body: json
10+
auth: none
11+
}
12+
13+
headers {
14+
Accept: application/json, text/event-stream
15+
Content-Type: application/json
16+
}
17+
18+
body:json {
19+
{
20+
"jsonrpc": "2.0",
21+
"id": "init-001",
22+
"method": "initialize",
23+
"params": {
24+
"protocolVersion": "2024-11-05",
25+
"capabilities": {
26+
"roots": {
27+
"listChanged": false
28+
},
29+
"sampling": {}
30+
},
31+
"clientInfo": {
32+
"name": "mcp-client",
33+
"version": "1.0.0"
34+
}
35+
}
36+
}
37+
}

bruno/mcp/2_initialized.bru

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
meta {
2+
name: 2. Send Initialized Notification
3+
type: http
4+
seq: 2
5+
}
6+
7+
post {
8+
url: http://localhost:9000/mcp
9+
body: json
10+
auth: none
11+
}
12+
13+
headers {
14+
Accept: application/json, text/event-stream
15+
Content-Type: application/json
16+
mcp-session-id:
17+
}
18+
19+
body:json {
20+
{
21+
"jsonrpc": "2.0",
22+
"method": "notifications/initialized"
23+
}
24+
}

bruno/mcp/3_list_tools.bru

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
meta {
2+
name: 3. List Tools
3+
type: http
4+
seq: 3
5+
}
6+
7+
post {
8+
url: http://localhost:9000/mcp
9+
body: json
10+
auth: none
11+
}
12+
13+
headers {
14+
Accept: application/json, text/event-stream
15+
Content-Type: application/json
16+
mcp-session-id:
17+
}
18+
19+
body:json {
20+
{
21+
"jsonrpc": "2.0",
22+
"id": "list-001",
23+
"method": "tools/list"
24+
}
25+
}

bruno/mcp/4_call_add.bru

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
meta {
2+
name: 4. Call Add Tool
3+
type: http
4+
seq: 4
5+
}
6+
7+
post {
8+
url: http://localhost:9000/mcp
9+
body: json
10+
auth: none
11+
}
12+
13+
headers {
14+
Accept: application/json, text/event-stream
15+
Content-Type: application/json
16+
mcp-session-id:
17+
}
18+
19+
body:json {
20+
{
21+
"jsonrpc": "2.0",
22+
"id": "add-001",
23+
"method": "tools/call",
24+
"params": {
25+
"name": "add",
26+
"arguments": {
27+
"a": 5,
28+
"b": 3
29+
}
30+
}
31+
}
32+
}

bruno/mcp/5_call_mul.bru

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
meta {
2+
name: 5. Call Multiply Tool
3+
type: http
4+
seq: 5
5+
}
6+
7+
post {
8+
url: http://localhost:9000/mcp
9+
body: json
10+
auth: none
11+
}
12+
13+
headers {
14+
Accept: application/json, text/event-stream
15+
Content-Type: application/json
16+
mcp-session-id:
17+
}
18+
19+
body:json {
20+
{
21+
"jsonrpc": "2.0",
22+
"id": "mul-001",
23+
"method": "tools/call",
24+
"params": {
25+
"name": "mul",
26+
"arguments": {
27+
"a": 4,
28+
"b": 7
29+
}
30+
}
31+
}
32+
}

bruno/mcp/bruno.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": "1",
3+
"name": "MCP Client",
4+
"type": "collection",
5+
"ignore": [
6+
"node_modules",
7+
".git"
8+
]
9+
}

labs/24-mcp.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Lab 24: Bonus: Model Context Protocol
2+
3+
In this lab, we will create an MCP server and connect to it using Bruno
4+
5+
## High level overview
6+
7+
1. Install `fastmcp` package
8+
1. Create a file `mcp_server.py`
9+
1. Create an `add` tool as follows
10+
11+
```python
12+
@mcp.tool()
13+
def add(a: int, b: int) -> int:
14+
"""Add two integers together.
15+
16+
Args:
17+
a: First integer
18+
b: Second integer
19+
20+
Returns:
21+
The sum of a and b
22+
"""
23+
return a + b
24+
```
25+
26+
1. Now similarly create another tool of your choice.
27+
- Make sure you put all the types and use descriptive names.
28+
- Don't forget to add the documentation string.
29+
- The decorator will read all these values and provide it to the client
30+
31+
1. Add code to run the server
32+
33+
```python
34+
if __name__ == "__main__":
35+
mcp.run(transport="streamable-http", host="127.0.0.1", port=9000)
36+
```
37+
38+
1. Now run the server in the terminal: ```python mcp_server.py```
39+
40+
Next, we will use Bruno to connect to this server
41+
42+
1. Load the collection which is in the directory `bruno/mcp` into Bruno
43+
1. Make a call to the first API. This API connects to the server and initialises the session
44+
1. The initialisation will return an MCP Session ID in the response headers. Copy it
45+
1. Now call the second API. In the headers, fill in the session ID that you copied from the initialisation
46+
1. Similarly call all the remaining APIs. Understand the input and output formats
47+
48+
These API calls happen in our agent code as well. When we configure the MCP server, openai agents sdk will
49+
50+
- Initialise the session
51+
- Get a list of tools
52+
- These tools will be added to the prompt automatically
53+
- When the LLM returns a tool call, the agent will make a call to the MCP server with the defined parameters to get the response

0 commit comments

Comments
 (0)