Skip to content

Commit fcb4057

Browse files
committed
cleanup
1 parent 128f320 commit fcb4057

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

01-logfire-hello-world/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logfire
22

3-
logfire.configure(service_name='hello-world')
3+
logfire.configure(environment='hello-world')
44

55
logfire.info('hello {place}', place='world')

02-pai-mcp-sampling/client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
from pydantic_ai import Agent
55
from pydantic_ai.mcp import MCPServerStdio
66

7-
logfire.configure(service_name='mcp-sampling-client')
7+
logfire.configure(service_name='mcp-client', environment='mcp-sampling')
88
logfire.instrument_pydantic_ai()
99
logfire.instrument_mcp()
1010

11-
THIS_DIR = Path(__file__).parent
12-
server = MCPServerStdio(command='python', args=[str(THIS_DIR / 'generate_svg.py')])
11+
server = MCPServerStdio(command='python', args=[str(Path(__file__).parent / 'generate_svg.py')])
1312
agent = Agent('openai:gpt-4.1-mini', mcp_servers=[server])
1413

1514

1615
async def main():
1716
async with agent.run_mcp_servers():
18-
result = await agent.run('Create an image of a robot in a punk style.')
17+
result = await agent.run('Create an image of a robot in a punk style, it should be pink.')
1918
print(result.output)
2019

2120

02-pai-mcp-sampling/generate_svg.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
import re
22
from pathlib import Path
33

4-
from mcp import SamplingMessage
4+
import logfire
55
from mcp.server.fastmcp import Context, FastMCP
66
from mcp.server.session import ServerSessionT
77
from mcp.shared.context import LifespanContextT, RequestT
8-
from mcp.types import TextContent
8+
from pydantic_ai import Agent
9+
from pydantic_ai.models.mcp_sampling import MCPSamplingModel
910

10-
app = FastMCP(log_level='WARNING')
11+
logfire.configure(service_name='mcp-server', environment='mcp-sampling', console=False)
12+
logfire.instrument_mcp()
1113

12-
import logfire
14+
app = FastMCP(log_level='WARNING')
1315

14-
logfire.configure(service_name='mcp-sampling-server', console=False)
15-
logfire.instrument_mcp()
16+
svg_agent = Agent(instructions='Generate an SVG image as per the user input. Return the SVG data only as a string.')
1617

1718

1819
@app.tool()
1920
async def image_generator(ctx: Context[ServerSessionT, LifespanContextT, RequestT], subject: str, style: str) -> str:
20-
prompt = f'{subject=} {style=}'
21-
# `ctx.session.create_message` is the sampling call
22-
result = await ctx.session.create_message(
23-
[SamplingMessage(role='user', content=TextContent(type='text', text=prompt))],
24-
max_tokens=1_024,
25-
system_prompt='Generate an SVG image as per the user input',
26-
)
27-
assert isinstance(result.content, TextContent)
21+
# run the agent, using MCPSamplingModel to proxy the LLM call through the client.
22+
svg_result = await svg_agent.run(f'{subject=} {style=}', model=MCPSamplingModel(ctx.session))
2823

2924
path = Path(f'{subject}_{style}.svg')
3025
# remove triple backticks if the svg was returned within markdown
31-
if m := re.search(r'^```\w*$(.+?)```$', result.content.text, re.S | re.M):
26+
if m := re.search(r'^```\w*$(.+?)```$', svg_result.output, re.S | re.M):
3227
path.write_text(m.group(1))
3328
else:
34-
path.write_text(result.content.text)
29+
path.write_text(svg_result.output)
3530
return f'See {path}'
3631

3732

0 commit comments

Comments
 (0)