You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Graphite has a built in react agent that can be used out of the box for your needs, we will use it for the simple use case of passing in input and retrieving the output from OpenAI.
131
131
132
132
```python
133
+
import asyncio
133
134
from grafi.agents.react_agent import create_react_agent
134
135
135
-
react_agent = create_react_agent()
136
+
async def run_agent():
136
137
137
-
result = react_agent.run(user_input, invoke_context)
138
+
react_agent = create_react_agent()
138
139
139
-
print("Output from React Agent:", result)
140
+
result = await react_agent.run(user_input, invoke_context)
2025-07-12 17:38:55.895 | DEBUG | grafi.common.instrumentations.tracing:setup_tracing:117 - OTLP endpoint is not available. Using InMemorySpanExporter.
249
-
2025-07-12 17:38:55.905 | INFO | grafi.common.topics.topic:publish_data:64 - [agent_input_topic] Message published with event_id: 1da4f45008264dc98fd63dc154dcaa6e
255
+
2025-07-12 17:38:55.905 | INFO | grafi.topics.topic:publish_data:64 - [agent_input_topic] Message published with event_id: 1da4f45008264dc98fd63dc154dcaa6e
250
256
2025-07-12 17:38:55.912 | DEBUG | grafi.nodes.node:invoke:49 - Executing Node with inputs: [ConsumeFromTopicEvent(event_id='8dd13f159fbc40b29ca030501f4d2859', event_version='1.0', invoke_context=InvokeContext(conversation_id='9051491ca7a84b71a3f3e9d790b79e4f', invoke_id='6e56da25ca824bf4a8d454a731b6f336', assistant_request_id='0e0317d25c9b40e4a76a130bbbb7bc43', user_id='9e396be8f8c442bc9269a31a9b3f69a2'), event_type=<EventType.CONSUME_FROM_TOPIC: 'ConsumeFromTopic'>, timestamp=datetime.datetime(2025, 7, 12, 16, 38, 55, 909090, tzinfo=datetime.timezone.utc), name='agent_input_topic', offset=0, data=[Message(name=None, message_id='ae2638fa5f3047fbbfb43dbd1545e6d2', timestamp=1752338335885634346, content='What is the capital of the United Kingdom', refusal=None, annotations=None, audio=None, role='user', tool_call_id=None, tools=None, function_call=None, tool_calls=None, is_streaming=False)], consumer_name='OpenAIInputNode', consumer_type='OpenAIInputNode')]
251
-
2025-07-12 17:38:57.400 | INFO | grafi.common.topics.topic:publish_data:69 - [function_call_topic] Message NOT published (condition not met)
252
-
2025-07-12 17:38:57.400 | INFO | grafi.common.topics.output_topic:publish_data:91 - [agent_output_topic] Message published with event_id: d83d3339fbd94d6e983fc51965177891
257
+
2025-07-12 17:38:57.400 | INFO | grafi.topics.topic:publish_data:69 - [function_call_topic] Message NOT published (condition not met)
258
+
2025-07-12 17:38:57.400 | INFO | grafi.topics.output_topic:publish_data:91 - [agent_output_topic] Message published with event_id: d83d3339fbd94d6e983fc51965177891
253
259
<span style="color:#FF4689">Output from React Agent:</span> The capital of the United Kingdom is London.
254
260
<span style="color:#FF4689">Events for conversation: </span> 9051491ca7a84b71a3f3e9d790b79e4f:
255
261
<span style="color:#FF4689">Events: </span>[AssistantInvokeEvent(event_id='7d97059ad3404c74be582ed4d01e365e', event_version='1.0', invoke_context=InvokeContext(conversation_id='9051491ca7a84b71a3f3e9d790b79e4f', invoke_id='6e56da25ca824bf4a8d454a731b6f336', assistant_request_id='0e0317d25c9b40e4a76a130bbbb7bc43', user_id='9e396be8f8c442bc9269a31a9b3f69a2'), event_type=<EventType.ASSISTANT_INVOKE: 'AssistantInvoke'>, timestamp=datetime.datetime(2025, 7, 12, 16, 38, 55, 885710), assistant_id='de51912ecc6c44ce803a0cd5eb358ba5', assistant_name='ReActAgent', assistant_type='ReActAgent', input_data=[Message(name=None, message_id='ae2638fa5f3047fbbfb43dbd1545e6d2', timestamp=1752338335885634346, content='What is the capital of the United Kingdom', refusal=None, annotations=No```</code></pre></div>
Copy file name to clipboardExpand all lines: docs/docs/guide/connecting-to-an-mcp-server.md
+17-12Lines changed: 17 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,10 +176,10 @@ from pydantic import Field
176
176
177
177
from grafi.assistants.assistant import Assistant
178
178
from grafi.assistants.assistant_base import AssistantBaseBuilder
179
-
from grafi.common.topics.input_topic import InputTopic
180
-
from grafi.common.topics.output_topic import OutputTopic
181
-
from grafi.common.topics.subscription_builder import SubscriptionBuilder
182
-
from grafi.common.topics.topic import Topic
179
+
from grafi.topics.input_topic import InputTopic
180
+
from grafi.topics.output_topic import OutputTopic
181
+
from grafi.topics.subscription_builder import SubscriptionBuilder
182
+
from grafi.topics.topic import Topic
183
183
from grafi.nodes.node import Node
184
184
from grafi.tools.function_calls.function_call_tool import FunctionCallTool
185
185
from grafi.tools.llms.impl.openai_tool import OpenAITool
@@ -337,7 +337,7 @@ class StockAssistantBuilder(
337
337
```
338
338
339
339
340
-
Graphtie is natively asychrnous, but you can chose to run syncrhous coroutines as well. For this case we are making a fully asynchronous workflow by overrding the `async def a_run()` method of the `Assistant` class. In order to run this create a `main.py` that will instantiate the assistant and execute it asynchrnously.
340
+
Graphtie is natively asychrnous, but you can chose to run syncrhous coroutines as well. For this case we are making a fully asynchronous workflow by overrding the `async def run()` method of the `Assistant` class. In order to run this create a `main.py` that will instantiate the assistant and execute it asynchrnously.
341
341
342
342
```python
343
343
#main.py
@@ -347,9 +347,10 @@ import uuid
347
347
from typing import Dict
348
348
349
349
from grafi.common.containers.container import container
350
-
from grafi.common.models.invoke_context import InvokeContext
351
-
from grafi.common.models.mcp_connections import StreamableHttpConnection
352
-
from grafi.common.models.message import Message
350
+
from grafi.common.events.topic_events.publish_to_topic_event import PublishToTopicEvent
351
+
from grafi.models.invoke_context import InvokeContext
352
+
from grafi.models.mcp_connections import StreamableHttpConnection
353
+
from grafi.models.message import Message
353
354
from grafi.tools.function_calls.impl.mcp_tool import MCPTool
@@ -524,11 +529,11 @@ Topics can have conditions attached to them to trigger them, in the case of `Inp
524
529
525
530
526
531
527
-
When you call the assistant's `a_invoke()` method from `main.py`
532
+
When you call the assistant's `invoke()` method from `main.py`
528
533
529
534
```python
530
535
531
-
async for output in assistant.a_invoke(invoke_context, input_data):
536
+
async for output in assistant.invoke(invoke_context, input_data):
532
537
```
533
538
534
539
The `input_data` gets published on the assistants `InputTopic` during workflow initialization. This is done [within the framework](https://github.com/binome-dev/graphite/blob/0b6b666b6f0e122ac970f437ee24f5d87c30a81a/grafi/workflows/impl/event_driven_workflow.py#L681-L687) but we are outlying it here so you can follow the flow of data.
With the `EventDrivenWorkflow` object created, we can invoke it by passing our `invoke_context` and a `List[Message]`. The workflow will execute and return the results, which we can then print. Save this complete code as `main.py`.
0 commit comments