Skip to content

Commit 128f320

Browse files
committed
initial commit
0 parents  commit 128f320

File tree

8 files changed

+1529
-0
lines changed

8 files changed

+1529
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv
11+
*.svg

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

01-logfire-hello-world/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import logfire
2+
3+
logfire.configure(service_name='hello-world')
4+
5+
logfire.info('hello {place}', place='world')

02-pai-mcp-sampling/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pathlib import Path
2+
3+
import logfire
4+
from pydantic_ai import Agent
5+
from pydantic_ai.mcp import MCPServerStdio
6+
7+
logfire.configure(service_name='mcp-sampling-client')
8+
logfire.instrument_pydantic_ai()
9+
logfire.instrument_mcp()
10+
11+
THIS_DIR = Path(__file__).parent
12+
server = MCPServerStdio(command='python', args=[str(THIS_DIR / 'generate_svg.py')])
13+
agent = Agent('openai:gpt-4.1-mini', mcp_servers=[server])
14+
15+
16+
async def main():
17+
async with agent.run_mcp_servers():
18+
result = await agent.run('Create an image of a robot in a punk style.')
19+
print(result.output)
20+
21+
22+
if __name__ == '__main__':
23+
import asyncio
24+
25+
asyncio.run(main())
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import re
2+
from pathlib import Path
3+
4+
from mcp import SamplingMessage
5+
from mcp.server.fastmcp import Context, FastMCP
6+
from mcp.server.session import ServerSessionT
7+
from mcp.shared.context import LifespanContextT, RequestT
8+
from mcp.types import TextContent
9+
10+
app = FastMCP(log_level='WARNING')
11+
12+
import logfire
13+
14+
logfire.configure(service_name='mcp-sampling-server', console=False)
15+
logfire.instrument_mcp()
16+
17+
18+
@app.tool()
19+
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)
28+
29+
path = Path(f'{subject}_{style}.svg')
30+
# remove triple backticks if the svg was returned within markdown
31+
if m := re.search(r'^```\w*$(.+?)```$', result.content.text, re.S | re.M):
32+
path.write_text(m.group(1))
33+
else:
34+
path.write_text(result.content.text)
35+
return f'See {path}'
36+
37+
38+
if __name__ == '__main__':
39+
# run the server via stdio
40+
app.run()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Demo of the Pydantic Stack

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[project]
2+
name = "pydantic-demo"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"logfire[httpx]>=3.21.1",
9+
"pydantic-ai>=0.3.4",
10+
]
11+
12+
[tool.ruff]
13+
line-length = 120
14+
target-version = "py39"
15+
16+
[tool.ruff.lint]
17+
extend-select = ["Q", "RUF100", "C90", "UP", "I"]
18+
flake8-quotes = { inline-quotes = "single", multiline-quotes = "double" }
19+
isort = { combine-as-imports = true }
20+
mccabe = { max-complexity = 15 }
21+
22+
[tool.ruff.lint.pydocstyle]
23+
convention = "google"
24+
25+
[tool.ruff.format]
26+
# don't format python in docstrings, pytest-examples takes care of it
27+
docstring-code-format = false
28+
quote-style = "single"

uv.lock

Lines changed: 1418 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)